I have a FragmentManager
that I am using to add Fragments
onto the backStack
.
FragmentTransaction ft = getFragmentManager().beginTransaction();
PUCCoursesSubjectList detail = new PUCCoursesSubjectList();
detail.item = item;
ft.add(R.id.container_frame, detail, "courses_detail");
ft.addToBackStack(null);
ft.commit();
Once I have a few Fragments
on the stack, I pop one off, but I need to know when the previous fragment is shown again.
It seems that onResume
doesn't apply here. Is there another method that I should be using to know when a Fragment
, that is already in the FragmentManger
, appears?
When you pop your fragment, you could get your other fragment from the fragment manager by the tag you provided and call a public method in your fragment.
YourFragment fragment = (YourFragment) getFragmentManager().findFragmentByTag("FRAGMENT_TAG");
if (fragment != null) {
fragment.callMethod();
}
The accepted answer could not work: getFragmentManager may return null in some case (ie Activity has been destroyed)
Now it's better to use Lifecycle, like in the next example:
if( getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED) )
{
// Do whatever you need
}
You can check by the tag if it is visible and not null by suing the tag
MyFragment myFragment = (MyFragment)getFragmentManager().findFragmentByTag("MY_FRAGMENT");
if (myFragment != null && myFragment.isVisible()) {
// add your code here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.