How to "destroy" several Android actions at the same time

I have a view of the wizard in the application, through 6 activities.

therefore I call:

Main activity - Call option 1 - Call option 2 - Call option 3 - Call option 4 - Call option 5

Now, in Option 5, I am saving the entire action in the database, and at this point I need to return to the main activity and destroy Option1,2,3,4 and 5.

Until option 5 saves the database, I need to go back, make changes, go to option 5 and save it.

The right way to do this is to somehow create a method that would have:

private void cleanStack(){ Option1.finish(); Option2.finish(); Option3.finish(); Option4.finish(); Option5.finish(); } 

And then start (or resume) the main activity?

Tpx

+4
source share
2 answers

What would I do, and not finish all the activities, create an intention to call back to my MainActivity.

Use the setFlags method to give this intent FLAG_ACTIVITY_CLEAR_TOP .

This will check your stack to make sure that the MainActivity instance already exists, and if it does, it will bring this action to the foreground and clear all actions on it instead of restarting MainActivity and placing it on top of the stack.

You may need to update the data if MainActivity requires information from parameters 1-5, since CLEAR_TOP in most cases will lead to turning the old MainActivity instance into focus, rather than completely recreating it ( onCreate will not be called, but onStart and onResume will).

Here's the documentation in the Intent class. There are other flags that can help you navigate if you are stuck. Good luck

+3
source

To clear the stack, use the following:

 Intent intent = new Intent ( this , MainActivity.class ); intent.addFlags ( Intent.FLAG_ACTIVITY_CLEAR_TOP ); startActivity ( intent ); 

Thus, since you have on the stack: Main activity → Call option 1 → Call option 2 → Call option 3 → Call option 4 → Call option 5

If you start MainActivity using the clear top flag, all actions on the stack on top of MainActivity will be completed.

+6
source

Source: https://habr.com/ru/post/1435941/


All Articles