Reputation: 1510
I have sort of wizard in app going through 6 Activities.
so I call:
Main Activity - Call Option 1 - Call Option 2 - Call Option 3 - Call Option 4 - Call Option 5
Now, on Option 5 I perform save of the whole action to database, and at that point I need to go back to Main Activity and destroy Option1,2,3,4 and 5.
Until Option 5 saves to database, I need to be able to go back, do changes, go forth to Option 5 and save it.
IS proper way to do it that I somehow create method that would have:
private void cleanStack(){
Option1.finish();
Option2.finish();
Option3.finish();
Option4.finish();
Option5.finish();
}
And then start (or resume) Main Activity?
Tnx
Upvotes: 5
Views: 1228
Reputation: 5178
What I would do is, rather than finishing all the Activities, create an Intent to call back to your MainActivity.
Use the setFlags
method to give this Intent the FLAG_ACTIVITY_CLEAR_TOP
.
This will check your stack to see if an instance of MainActivity already exists, and if it does it'll bring that Activity to the front and clear all the Activities above it instead of restarting MainActivity and putting it on top of the stack.
You may need to refresh the data, if MainActivity requires info from Options 1-5, as CLEAR_TOP will, in most cases, bring the old instance of MainActivity into focus rather than completely recreating it (onCreate
will not be called, but onStart
and onResume
will).
Here's the documentation on the Intent class. There are other flags that can help you with navigation if you get stuck. Good luck!
Upvotes: 3
Reputation: 4383
Use the following to clear the stack :
Intent intent = new Intent ( this , MainActivity.class );
intent.addFlags ( Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity ( intent );
In this manner, since in the stack you have : Main Activity -> Call Option 1 -> Call Option 2 -> Call Option 3 -> Call Option 4 -> Call Option 5
If you start the MainActivity using the clear top flag, all activities in the stack on top of the MainActivity will be finished.
Upvotes: 7