Y
Y
yaAmpa2015-08-19 15:41:28
Android
yaAmpa, 2015-08-19 15:41:28

How to close all sequentially opened fragments one after another?

There are several fragments.
Each one is pushed onto the stack when opened. The following code is used to display each fragment:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
        transaction.replace(containerId, fragment);
        transaction.addToBackStack(null);
        transaction.commitAllowingStateLoss();

Fragment i opens a new fragment i + 1 , which opens the next fragment i + 2 and so on. At a certain point in time, the last open fragment receives a message that it and all previous fragments up to the i-th must be closed. How can you conveniently organize the storage of a list of all fragments that should be closed? Or is it possible to somehow pass information to the previous fragment that it should be closed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LeEnot, 2015-08-19
@LeEnot

You are adding fragments to the stack incorrectly. The method transaction.addToBackStack(null)you pass nullto must accept an identifier string by which you can uniquely identify the given fragment on the stack. You can return to any fragment marked like this like this:
where name is just the same identifier string.
calling

FragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
you can clear the entire stack of fragments.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question