Fragment not calling onPause or onStop when using replace

I have this weird problem that my fragments do not call any end lifecycle methods like onPause and onStop when I replace it with another fragment. I replace the fragment as follows

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 this somehow supports my fragment, even if I popBackStack is called after replacing the same fragment again after it was shown before, and then it calls onStop before onStart?

+7
android android-fragments back-stack fragmentmanager
source share
2 answers

I solved the problem :)

This was because my application on the tablet added two fragments to one main fragment, and when I replaced the fragment containing two other fragments, their onPause and onStop are not called that way, calling them onStop in the main fragments of onStop, which I solved this problem.

Thanks to everyone who tried to help, I did not know that the method I used was really a problem, but these child fragments were not destroyed along with my parents!

+4
source share

Using your exact code inside an abstract class, I cannot reproduce this problem. I created an abstract class, ReplaceFragment .

My Main class extends FragmentActivity and sets the content view for the fragment.

Inside the Fragment class, I set up a ListView . When the list item is clicked, I do the following:

  getListView().setItemChecked(index, true); // Check what fragment is currently shown, replace if needed. DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details); if (details == null || details.getShownIndex() != index) { details = DetailsFragment.newInstance(index); ReplaceFragment.replaceFragment(...); } 

My output in LogCat every time I clicked:

10-07 12: 19: 07.688: V / FragmentManager (861): remove: DetailsFragment {40527d48 # 1 id = 0x7f040003} nesting = 2

10-07 12: 19: 07.688: V / FragmentManager (861): movefrom RESUMED: DetailsFragment {40527d48 # 1 id = 0x7f040003}

10-07 12: 19: 07.688: E / DetailsFragment (861): Details onPause ()

10-07 12: 19: 07.688: V / FragmentManager (861): movefrom STARTED: DetailsFragment {40527d48 # 1 id = 0x7f040003}

10-07 12: 19: 07.688: E / DetailsFragment (861): Details onStop ()

10-07 12: 19: 07.699: V / FragmentManager (861): movefrom STOPPED: DetailsFragment {40527d48 # 1 id = 0x7f040003}

Submit additional information about your implementation to help you further.

+2
source share

All Articles