As you know, a fragment has its own life cycle, and you can use its event from the lifecyle life whenever you want.
But the life cycle of fragments depends on the life cycle of activity. Therefore, when an action is destroyed, fragments are also destroyed.
// Create new fragment and transaction Fragment newFragment = new ExampleFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit();
Yo can use fragment transaction to replace them in one action, I think you are using the same,
I think that you are mistaken, and it makes no sense to use a fragment instead of using different types of activity.
But you have to think, you really have to use them in one activity? If you do not need to show them in one action, you do not need to use a snippet.
A fragment must always be embedded in activity, and a fragment life cycle directly depends on the host life cycle. For example, when an action is paused, all fragments in it and when the action is destroyed, as well as all fragments. However, although the action is being performed (it is in the state of a renewed life cycle), you can independently process each fragment, for example, add or remove them. When you perform such a fragment transaction, you can also add it to the back, which is controlled by activity - each entry of the back stack into the activity is a record of the fragment transaction that has occurred. The back stack allows the user to cancel a fragment transaction (move backward) by clicking the back button.
As a result, this is not so, but I think that if you do not need to, creating another activity is easy to support.
