How to pause/resume a fragment

android, android-fragments

Solution

Sounds like you want to call FragmentTransaction#attach and FragmentTransaction#detach to put your fragment through the life-cycle routines the same as FragmentPagerAdapter (see source here).

Detaching the `Fragment` with `detach()` will put the `Fragment` through the `onPause`, `onStop` and finally `onDestroyView` life-cycle methods, and then when you re-attach it with `attach()` it will go through `onCreateView`, `onStart` and finally `onResume` life-cycle methods.

You must make sure you are using tag's as-well as container-id since you can have multiple fragments attached to a single container and you will have to be able to get the `Fragment` reference from the `FragmentManager` which will then have to be done via its tag.

Problem

Background: I wrote a custom container which is capable of holding three fragments. Depending on the state of this container only two or those three fragments are visible. To inform fragments that their visibility was changed I tried out two options: I called Fragment.setUserVisibleHint() method with respective value. Hosted fragments overrode this method and react appropriately. This worked out. I called FragmentTransaction.hide() and FragmentTransaction.show() methods to hide and show fragments. Hosted fragments overrode Fragment.onHiddenChanged() and reacted as needed. This worked out as well. My problem is that I am not satisfied with either of these options. I would like to put invisible fragment into a standard paused state. Advantage of this option is that I keep the code clean and simple, as I don't need to override any special methods (like `setUserVisibleHint()` or `onHiddenChanged()`) and I can handle everything inside `onPause()` and `onResume()` which are already implemented. Question: What is the proper way to put a fragment into a paused state and then to resume it from that state? Update: I tried out `FragmentTransaction.detach()` too. This is not an option because it destroys the view, which is not allowed in my case.

Original source