2

I'm navigating from:

  • Main activity to Activity 2
  • Activity 2 to Activity 3
  • Activity 3 to Activity 4

through Intent.

I've also created the menu so that user can directly navigate from Activity 4 to Main activity.
But after navigating from Activity 4 to Main activity by using menu, when I press back, it takes me to Activity 3 rather than exiting the application.

I tried:

@Override
public void onBackPressed() {
    super.onBackPressed();
    MainActivity.this.finish();
}

But no gain. Any suggestions?

GAMA
  • 5,958
  • 14
  • 79
  • 126

8 Answers8

6

Just try this code when you navigate from 4th Activity to Main Activity.

Intent inMain=new Intent(Activity4.this, MainActivity.class);
inMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(inMain);

Note:

This code in your case, clears the previous activities and launches the main activity with no activity in the backstack.

Pattabi Raman
  • 5,814
  • 10
  • 39
  • 60
Deepika
  • 566
  • 2
  • 10
3

You can set flag Intent.FLAG_ACTIVITY_CLEAR_TOP to finish all the intermediate activities and your called activity will come to top of the activity stack.

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
2

use the flag Intent.FLAG_ACTIVITY_CLEAR_TOP with your intent. For more detail -

FLAG_ACTIVITY_CLEAR_TOP

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
1

call startActivity Method using clear top flag

   startActivity(new Intent(this, UI.class)
  .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
Antony
  • 603
  • 6
  • 14
1

You need to consider your "back stack" more closely.

What is happening exactly is, your back stack gets populated as follows:

Main activity -> Activity 2 -> Activity 3 -> Activity 4

Then from Activity 4 you launch your Main Activity. So your stack becomes:

Main activity -> Activity 2 -> Activity 3 -> Main Activity

Hence, when you press back, you land up in Activity 3.

Solution:

Either call finish() on every Activity when you navigate away from them. Or use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear all intermediate Activities.

More about Intent.FLAG_ACTIVITY_CLEAR_TOP.

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
1

When you go back to the MainActivity you need to use the Intent.FLAG_ACTIVITY_CLEAR_TOP on your Intent. This is an example of a goHome method that you can used in your Activity:

public void goHome ()
{
    Intent homeIntent = new Intent();
    homeIntent.setClass(this, MainActivity.class);
    homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(homeIntent);
}
monchote
  • 3,440
  • 2
  • 20
  • 20
0

In activity 2 and 3, after calling startActivity(intent), call this.finish(). This will also be one solution.

Guanlun
  • 1,598
  • 2
  • 18
  • 29
  • no, it'll not work in my case as if I want to get back to 2 from 3, I can't navigate – GAMA Jun 20 '12 at 09:15
0

Finish previous activity when you are go to the next activity means write finish(); after startactivity(intent); and write below code for start first activity from fourth activity's button click event.

Intent in1=new Intent(Act4.this, Act1.class);
startActivity(in1);
finish();
  • no, it'll not work in my case as if I want to get back to 2 from 3, I can't navigate – GAMA Jun 20 '12 at 09:15
  • @GAMA you can navigate to activity3 to activity2 using intent. –  Jun 20 '12 at 09:19
  • no, not using intent. i'm talking about going to act 3 from act 2 by using back button. – GAMA Jun 20 '12 at 09:24
  • @GAMA Using this code you are going to act3 to act2, Intent in1=new Intent(Act3.this, Act2.class); startActivity(in1); finish(); –  Jun 20 '12 at 09:27
  • NO, i'm not navigating from 3 to 2 by code. That should be taken care by back button and hence I'm not using `finish()` – GAMA Jun 20 '12 at 09:31