Issue
WHAT I HAVE
I have a Fragment called MainFragment. This is added from my MainActivity. Within that Fragment in on its onResume method I add another Fragment called SecondFragment like so.
@Override
public void onResume() {
super.onResume();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
SecondFragment secondFragment = (SecondFragment) fragmentManager.findFragmentByTag(SecondFragment.FRAG_TAG);
if (secondFragment == null)
secondFragment = SecondFragment.newInstance();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(mContainer.getId(), secondFragment, SecondFragment.FRAG_TAG).commit();
}
I then remove it like so.
@Override
public void onPause() {
super.onPause();
FragmentManager fm = getActivity().getSupportFragmentManager();
SecondFragment mhpf = (SecondFragment) fm.findFragmentByTag( SecondFragment.FRAG_TAG );
if ( mhpf != null )
fm.beginTransaction().remove( mhpf ).commit();
}
THE ISSUE
This all works well and good but it doesn't fit in with the lifecycle of the Fragment when things like orientation changes happen or when the user goes home and then opens the application up again.
WHY I'M DOING IT LIKE THIS AND NOT ADDING IT FROM THE ACTIVITY
This is part of an Android Library Module so I want it self contained. The user just has to add the MainFragment and then the SecondFragment is handled entirely by the MainFragment.
MY QUESTION
Is there a way to get my SecondFragment to behave like a normal Fragment without having to add it and remove it from and Activity?
Solution
Use getActivity().getChildFragmentManager(); in place of getActivity().getSupportFragmentManager();
If you are using Fragment inside Fragment.
Answered By - Pradeep Kumar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.