Cannot call getSupportFragmentManager() from activity
Your activity doesn't extend FragmentActivity from the support library, therefore the method is not present in the superclass
If you are targeting api 11 or above, you could use Activity.getFragmentManager instead.
How to invoke getSupportFragmentManager() or getFragmentManager() in Activity?
Use AppCompatActivity
instead of Activity
to use getSupportFragmentManager()
in Your Activity
and use getFragmentManager()
or getActivity().getSupportFragmentManager()
in fragment
how to use getSupportFragmentManager() in Activity
You can't. getSupportFragmentManager()
is only available in FragmentActivity and classes that extend it.
From the docs, you can see that AppCompatActivity is a Activity, so everything in Activity
is also available in AppCompatActivity
.
java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity
↳ android.support.v4.app.FragmentActivity
↳ android.support.v7.app.AppCompatActivity
Error calling getSupportFragmentManager() from a Broadcast receiver
To Call the method which displays my BottomSheet which is situated in the Activity that is currently displayed in the UI from a background service. I used MutableLiveData.
I initialised a global Boolean finished, in my service;
public static MutableLiveData<Boolean> finished = new MutableLiveData<Boolean>();
When the required event triggers in my Service i set the value of the live data variable to true. with
finished.setValue(true);
In the activity i need to display the BottomSheet i am observing this variable via:
finished.observe(this, new Observer<Boolean>(){
@Override
public void onChanged(Boolean aBoolean){
if(aBoolean){
//I call my Method here which displays my Bottom Sheet, this is the method that contains the getSupportFragmentManager().
}
}
});
I hope this can help anyone in my situation.
How can I access getSupportFragmentManager() in a fragment?
You can directly call
getParentFragmentManager()
to get the fragment manager. Note that getFragmentManager() also works but has been marked as deprecated.
Related Topics
How to Use an Android Handler to Update a Textview in the UI Thread
Creating Different Layout for Android Phone and Tablet
Android - Detecting Application Launch from Home or History
How to Disable Proguard for Building My Android App
What Are the Overheads of Using Autoincrement for SQLite on Android
How to Get a Fragment to Remove Itself, I.E. Its Equivalent of Finish()
Android Multiple Screen Sizes with Same Density
How to Programmatically Log Out from Facebook Sdk 3.0 Without Using Facebook Login/Logout Button
Running Emulator After Building Android from Source
Android Drag/Animation of Views
Disable Home Button in Android Ics (4.0)
Handle a View Visibility Change Without Overriding the View
Android 2.1 View's Getdrawingcache() Method Always Returns Null
Android 3.1.1 - Failed Resolution Of: Lcom/Google/Android/Gms/Common/Internal/Zzbq;
Should Accessing Sharedpreferences Be Done Off the UI Thread
Estimating Beacon Proximity/Distance Based on Rssi - Bluetooth Le
Android: How Handle Message Error from the Server Using Volley