12

In my custom view I have a Java class running a simple game. When the game is finished I'd like to display my DialogFragment, but the getFragmentManager() method seems to be undefined.

FragmentManager manager = getFragmentManager();
Finish finish = new Finish();
finish.show(manager, "done");

I've tried getting the manager through a Fragment obj as:

Fragment fragment = new Fragment();
FragmentManager manager = fragment.getFragmentManager();

But that returns as null. I know it's a new Fragment instance, but I'm not sure what value to give it. Any help would be much appreciated.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
  • 1
    Your `View` should not be directly interacting with `FragmentManager`. Instead, create an interface that your `Activity` can implement, by which your `View` can signal that the game is done, and do the `FragmentTransaction` in the `Activity`'s interface method. – Mike M. Apr 18 '16 at 02:31
  • So something like a static method in the parent view, and call it to signal the game is finished (and proceed with my fragment there)? –  Apr 18 '16 at 02:34
  • No. You won't be able to access the `FragmentManager` from a `static` method, without some shaky parameter relaying. Use an `interface`. – Mike M. Apr 18 '16 at 02:43
  • Okay I'm just a little fuzy on how to signal. So if I make the interface, and implement it in the Activity, how would my View be able to use that interface to signal the Activity? –  Apr 18 '16 at 02:51
  • You would pass the `Activity` to the `View` as the interface type, and call the interface method on it when the game is done. Have a look here: http://stackoverflow.com/questions/994840/how-to-create-our-own-listener-interface-in-android – Mike M. Apr 18 '16 at 02:54
  • 1
    Thanks so much it's working great now! –  Apr 18 '16 at 03:13

4 Answers4

13

if your view is attached to an Activity then simply you can do

((Activity)getContext()).getFragmentManager();

or

((ActivityCompat)getContext()).getSupportFragmentManager();

and to be more safe please make sure you check against of the View Context is instance of an Activity by doing such:

if(getContext() instanceof Activity)// do something

and a better solution is, i had rely on a callback between the View and the Activity.

Kosh
  • 6,140
  • 3
  • 36
  • 67
8

I use that simple helper method:

fun getFragmentManager(context: Context?): FragmentManager? {
    return when (context) {
        is AppCompatActivity -> context.supportFragmentManager
        is ContextThemeWrapper -> getFragmentManager(context.baseContext)
        else -> null
    }
}
Anton Kaliturin
  • 147
  • 1
  • 6
  • Note: This won't work for Hilt. In Hilt, the type of the context that is passed to the Fragments' child views is FragmentContextWrapper, Hilt's internal type. – Brian Oh Feb 03 '23 at 07:01
0

For AndroidX, you can try:

ContextThemeWrapper themeWrapper = (ContextThemeWrapper) getContext();
FragmentManager fm = ((AppCompatActivity) themeWrapper.getBaseContext()).getSupportFragmentManager();

As recommoned by @k0sh, instaceof safety check is recommonded.

Ankur
  • 1,268
  • 18
  • 22
-1

You can use this (in Kotlin):

if ((context as ContextThemeWrapper).baseContext is AppCompatActivity) {
            //View is attached to an AppCompatActivity
        } else {
            //View is not attached to an AppCompatActivity
        }
}
Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103