8

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?

JWqvist
  • 717
  • 6
  • 15
  • Where do you call this method in your code ? – GrIsHu Oct 02 '13 at 09:04
  • Its in an abstract class – JWqvist Oct 02 '13 at 09:10
  • Could you show us the code for one of your impacted fragments? Does it have the @Override directive on onPause()? – Pork 'n' Bunny Oct 07 '13 at 08:13
  • Enable logging with FragmentManager.enableDebugLogging(true); and see if that shows anything useful – Delyan Oct 07 '13 at 08:23
  • It have the @Override and the DebugLogging is telling me the same as in my question :( – JWqvist Oct 07 '13 at 09:46
  • 2
    Any chance you've overriden life cycle methods (either in your `Activity` or `Fragment` classes) and forgotten to delegate the call to the `super`? There is nothing wrong with the code snippet in your question, so the problem is probably somewhere in the hosting `Activity` or one of your `Fragment` classes. Without any more details I'm afraid is going to be impossible to further pinpoint the exact issue. – MH. Oct 08 '13 at 07:18

2 Answers2

6

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!

JWqvist
  • 717
  • 6
  • 15
2

Using your exact code inside of an abstract class, I can't replicate this issue. I created an abstract class, ReplaceFragment.

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

Inside the Fragment class, I set up a ListView. When a list item is clicked, I'm doing 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 on each click is:

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}

Post further details of your implementation so that can help you further.

Community
  • 1
  • 1
Mike P.
  • 1,920
  • 1
  • 18
  • 20
  • I have only one Activity in my application so I use this method to replace the fragments inside a container (framelayout). – JWqvist Oct 07 '13 at 13:38
  • The onPause and onStop of the fragment is called when the Activitys onPause or onStop is called. But not when I am replacing it. – JWqvist Oct 07 '13 at 13:41
  • Post additional source and I'll be happy to test your exact use case. – Mike P. Oct 07 '13 at 13:57
  • Well its a normal class that extends fragment. Try to remove the extends FragmentActivity and the extends ReplacementFragment and you have what i have. – JWqvist Oct 07 '13 at 14:06
  • I am not implementing it its an abstract class and the method is static so I just call ClassName.replaceFragment(...); – JWqvist Oct 07 '13 at 14:20
  • I call the method in a onClick method in a menu in the left side of the screen – JWqvist Oct 07 '13 at 14:23
  • Okay, I appear to have nearly your exact configuration here, and I cannot reproduce the issue. – Mike P. Oct 07 '13 at 14:28
  • I recall that it have worked before and now it does not. Something have broken it and that is the question why. Does it have anything to do with the manifest or the fragmentmanager? thanks for the help but it seams to be a deeper problem :( – JWqvist Oct 07 '13 at 14:52
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38729/discussion-between-mike-and-jeppe-andersen) – Mike P. Oct 07 '13 at 15:33