Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finish all previous activities

My application has the following flow screens :

Home->screen 1->screen 2->screen 3->screen 4->screen 5

Now I have a common log out button in each screens

(Home/ screen 1 / screen 2 /screen 3/ screen 4 / screen 5)

I want that when user clicks on the log out button(from any screen), all the screens will be finished and a new screen Log in will open .

I have tried nearly all FLAG_ACTIVITY to achieve this. I also go through some answers in stackoverflow, but not being able to solve the problem. My application is on Android 1.6 so not being able to use FLAG_ACTIVITY_CLEAR_TASK

Is there any way to solve the issue ?

like image 989
Tanmay Mandal Avatar asked Jun 13 '11 12:06

Tanmay Mandal


People also ask

How do you finish an activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do I close all activities on Android?

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

How do I go back to previous activity on Android?

In the second activity, the back button at the top left can be used to go back to the previous activity.


1 Answers

Use:

Intent intent = new Intent(getApplicationContext(), Home.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 

This will clear all the activities on top of home.

Assuming you are finishing the login screen when the user logs in and home is created and afterward all the screens from 1 to 5 on top of that one. The code I posted will return you to home screen finishing all the other activities. You can add an extra in the intent and read that in the home screen activity and finish it also (maybe launch login screen again from there or something).

I am not sure but you can also try going to login with this flag. I don't know how the activities will be ordered in that case. So don't know if it will clear the ones below the screen you are on including the one you are currently on but it's definitely the way to go.

like image 200
DArkO Avatar answered Oct 14 '22 01:10

DArkO