androidnavigationfragmentdrawer

Android Change main Fragment from child


I'm having some troubles with my Fragments management..
It's quite new for me and I'm pretty sure you'll find an easy way to solve my problem.

So I have a NavigationDrawer in a FragmentActivity and from this, I can navigate between two fragments.
One of those two fragments has children in a ViewPager.
I'd like to change the Parent Fragment from a ChildFragment which is in the ViewPager..

I'd like to load FragmentA from Fragment2B


Solution

  • Just perform a normal FragmentTransaction and be sure to use the FragmentManager of the Activity!

    FragmentManager manager = getActivity().getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(...);
    transaction.commit();
    

    If your Activity is a FragmentActivity or ActionBarActivity you have to remember to use the support FragmentManager, aside from that everything stays the same:

    FragmentManager manager = getActivity().getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(...);
    transaction.commit();