Tran Triet Tran Triet - 24 days ago 7
Android Question

Stop user from going back to MANY previous activities in Android

I am have an Android app with 3 Activities.

Activity 1:

--Button Signup1

--Button Signup2

Activity 2:

--Button CreateAccount

Activity 3:

The flow goes like this:

Activity_______UserAction____________Result

1____________click SignUp1____________Move to Act2

2____________click CreateAccount_____Move to Act3

3

What I want to know is when I got to Activity 3, how can I prevent user from going back to ALL the previous activities, not just the immediate previous one (activity2).

I have looked around and people have always recommended calling finish() after startActivity() in Activity2. But that won't work since android will direct user back to activity1.

In addition to that, in Activity1, I don't want to call finish() after startActivity() because I want to let users come back to Activity1 from Activity2 (say, they want to use SignUp2 instead).

To sum up, only when the user reaches Activity3, should they be prevented from going back to any other previous activities.

How do I achieve that?

Answer

Start your activity with these flags

(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK)