Reputation: 1069
In my application I have two activities
both having a static method. I call these methods from a separate activity. The problem is that in my static methods I am unable to get access to getSupportFragmentManager();
the error being produced is:
"unable to resolve method getSupportFragmentManager()"
So how do I resolve this problem?
Upvotes: 5
Views: 4163
Reputation: 123
you can try this code. I think this useful for you.
FragmentManager ft = ((FragmentActivity)activity).getSupportFragmentManager();
DialogFragment newFragment = MyNotification.newInstance();
newFragment.setCancelable(false);
newFragment.show(ft, "mydialog");
Upvotes: 1
Reputation: 29794
In my application I have two activities both having a static method. I call these methods from a separate activity. The problem is that in my static methods I am unable to get access to getSupportFragmentManager();
Using a static method in an Activity which access an active part of it is usually a bad practice. It is because you can't ensure that all the activity part is still intact when in inactive state. But you can use a static method for intent creation in activity, something like this:
public static Intent createIntent(Activity activity, int value) {
Intent intent = new Intent(activity, YourActivityClass.class);
intent.put("EXTRA_VALUE", value);
return intent;
}
which will hide all the extra key when you're trying to create an intent to launch an activity. Where you can call it with:
Intent intent = YourActivityClass.createIntent(this, 10);
startActivity(intent);
the error being produced is "unable to resolve method getSupportFragmentManager()". So how do I resolve this problem?
You need to modify your static method to directly required a FragmentActivity as its parameter. It is because getSupportFragmentManager() is part of FragmentActivity. Something like this:
public static void doSomething(FragmentActivity activity) {
SupportFragmentManager manager = activity.getSupportFragmentManager();
...
}
But with the above code, you can't access the SupportFragmentManager from the activity of the static method. So, it's kinda useless.
Or you can try:
public static SupportFragmentManager doSomething() {
return activity.getSupportFragmentManager();
}
But it will give you NullPointerException when the activity is destroyed or inactive (not always).
Upvotes: 1