Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment not calling onPause or onStop when using replace

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?

like image 636
JWqvist Avatar asked Oct 02 '13 08:10

JWqvist


People also ask

How do you replace fragments?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();

Is onPause always called before onStop?

onPause() is always called. This is guaranteed. If you need to save any state in your activity you need to save it in onPause() . onStop() may be called after onPause() , or it may not.

Which method is called when fragment is replaced?

But you can create your own interface called Replaceable with a method onReplace() that you have your fragment implement, and call it directly when you call FragmentTransaction. replace() .

How do you refresh a fragment?

Go to your navigation graph and find the fragment you want to refresh. Create an action from that fragment to itself. Now you can call that action inside that fragment like so.


2 Answers

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!

like image 103
JWqvist Avatar answered Sep 29 '22 22:09

JWqvist


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.

like image 33
Mike P. Avatar answered Sep 25 '22 22:09

Mike P.