0

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?

Tran Triet
  • 1,257
  • 2
  • 16
  • 34
  • Please explain the downvote so I know where I messed up. – Tran Triet Nov 15 '16 at 11:29
  • override the `onBackPress` – Enzokie Nov 15 '16 at 11:31
  • @Enzokie i don't think this is what he was looking for, he needs the vlear top flag on startactivity. this is why i flagged as duplicate :) – Pier Giorgio Misley Nov 15 '16 at 11:32
  • Thanks for the link. I'm checking it out. The problem there seems like mine. Wonder why it did not pop up when I tried to look through the similar problem! – Tran Triet Nov 15 '16 at 11:33
  • @PierGiorgioMisley I find the question hard to understand and this line from the op: `I want to let users come back to Activity1 from Activity2` makes me think of that usecase. :) – Enzokie Nov 15 '16 at 11:33
  • @Enzokie yeah the lack of indentation makes that question hard to understand. The problem was the step from activity2 to activity3 which has to remove the backstack way to prevent to go back to others activities – Pier Giorgio Misley Nov 15 '16 at 11:36
  • 1
    Thanks @PierGiorgioMisley. It is indeed what I was looking for! I did try to look through old questions but I just didn't see this one. I'll try to look harder for similar posts from now on. – Tran Triet Nov 15 '16 at 11:40

4 Answers4

4

You can try adding this flags to Activity3 intent:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_A‌​CTIVITY_NEW_TASK);

This should force to clear Activity backstack

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
  • I just tried this out myself because I was interested in the approach, but I'm still able to go back to my activity 2 using the back button despite setting the suggested flags on the intent used to start my activity 3. I'm guessing it's because the task activities 1 and 2 are running in sticks around when the new task is opened for activity 3, so can still be gotten back to using the back button from activity 3. Any ideas? – clownba0t Nov 15 '16 at 12:03
1
 @Override
public void onBackPressed() {
    super.onBackPressed();}

using this you can exactly tell android what to do in case of back pressed.

Also it would a good idea if u could override your onPause and onDestroy method as per your requirements,please go through android lifecycle for better clarity on this

Ak9637
  • 990
  • 6
  • 12
1

If you're only worried about navigation between the activities using the device's back button then, given the limitations as presented, you should be able to achieve what you want by overriding Activity's onBackPressed() method. Make sure the implementation is empty, like this:

@Override public void onBackPressed() { /* Empty */ }

This will prevent Android's standard implementation of onBackPressed(), which finishes the activity, from running.

clownba0t
  • 1,103
  • 10
  • 12
1

Start your activity with these flags:

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

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
vaibhav kumar
  • 414
  • 2
  • 11
  • Thanks everyone. I +1 all your answers because they were all on spot in one way or another. However, since this is the first one, I think it's only right I choose this to be the answer. – Tran Triet Nov 15 '16 at 11:47