Fragment not calling onPause or onStop when using replace

JWqvist picture JWqvist · Oct 2, 2013 · Viewed 8.8k times · Source

I have this odd problem that my fragments are not calling any of the end lifecycle methods like onPause and onStop when I replace it with another fragment. I replace the fragment like this

public static void replaceFragment(Activity activity, int layoutId, Fragment fragment, String title, String shortTitle) {
    FragmentTransaction transaction = activity.getFragmentManager().beginTransaction().replace(layoutId, fragment);
    transaction.addToBackStack(title);
    transaction.setBreadCrumbTitle(title);
    transaction.setBreadCrumbShortTitle(shortTitle);
    transaction.commit();
    activity.getFragmentManager().executePendingTransactions();     
}

I think it is somehow keeping my fragment alive even if i popBackStack cause after replacing the same fragment again after it have been shown ones before then it also calls the onStop before onStart?

Answer

JWqvist picture JWqvist · Oct 14, 2013

I solved the problem :)

It was because my app on tablet added two fragments in one main fragment, and when I then replaced the fragment containing the two other fragment theirs onPause and onStop are not called so by calling theirs onStop in the main fragments onStop I have solved this problem.

Thanks to all for trying to help out i did't know that the method I was using was'n really the problem but that child fragments not are destroyed with their parent!