Cannot Call Getsupportfragmentmanager() from Activity

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



Leave a reply



Submit