0

How to add getSupportFragmentManager inside the Fragment?

    android.support.v4.app.FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.replace(R.id.calendar1, caldroidFragment);
    t.commit();

getActivity.getSupportFragmentManager is not accepted. Thank you.

jvpintang
  • 51
  • 8
  • 1
    In a `Fragment`, it's just `getFragmentManager()`. Unless you're dealing with child `Fragment`s, in which case you'd use `getChildFragmentManager()`. – Mike M. Nov 12 '16 at 03:08
  • @MikeM. , Thanks, It's worked now! – jvpintang Nov 12 '16 at 03:33
  • No problem. I'll just vote this a duplicate so you can close out your question. Just select the "That solved my problem!" button. Thanks. Cheers! – Mike M. Nov 12 '16 at 06:47

2 Answers2

0

your Activity must the FragmentActivty from the support library.

FragmentActivity

support library setup

Matthew Shearer
  • 2,715
  • 3
  • 23
  • 32
0

You can have safety check first.

if (getActivity() instanceof FragmentActivity) {
    FragmentActivity activity = (FragmentActivity) getActivity();
    activity.getSupportFragmentManager();
}

We know that fragment is very dependent to Fragment activity so we can call it inside.

Enzokie
  • 7,365
  • 6
  • 33
  • 39