Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "destroy" multiple Android activities at same time

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

like image 282
Balkyto Avatar asked Sep 24 '12 14:09

Balkyto


People also ask

How do I close all activities on Android?

exit(); or finish(); it exit full application or all activity.

How is an activity killed in Android system?

Activity can't be killed but Os can kill the whole application. In this case you can try finish() / finishActivity() / context. finish() to finish the activity. While you finish an activity backpress will not return to the previous activity.

How do I start the same activity again on android?

If you just want to reload the activity, for whatever reason, you can use this. recreate(); where this is the Activity. This is never a good practice. Instead you should startActivity() for the same activity and call finish() in the current one.

How pass data from activity to services in Android?

Using putExtra() method, we can send the data. While using it, we need to call setResult() method in services. We can also store data in a common database and access it on services as well as in Activity.


1 Answers

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.

like image 113
Leeeeeeelo Avatar answered Sep 18 '22 05:09

Leeeeeeelo