2

I have an Activity which is NOT the main Activity of my application, but which is launchable via a shortcut. When launched directly from the shortcut it accepts some user input then launches the MainActivity using the code below:

    Intent myIntent = new Intent(
            QuickTimerActivity.this.getBaseContext(),
            MainActivity.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    QuickTimerActivity.this.startActivity(myIntent);

After calling the MainActivity I want the calling activity to disappear from the stack - i.e. when the user presses the back button on the MainActivity I want them to exit the app.

I've looked through other posts and tried adding various flags to my intent (including FLAG_ACTIVITY_CLEAR_TOP as shown above), but none of them have the desired effect. How can I remove the calling Activity from the stack, or alternatively have the user exit the application if the user presses the back button and the activity was called from the code above?

Ian M
  • 567
  • 8
  • 33

3 Answers3

3

Call

finish();

after sending the intent.

danny117
  • 5,581
  • 1
  • 26
  • 35
1

Yes,finish() will help u.Or u can use android:noHistory flag for first activity in manifest.This will call the finish() method.

oops! sry Nibha already said...

Kavi Kumar
  • 31
  • 1
0
Intent myIntent = new Intent(
            QuickTimerActivity.this.getBaseContext(),
            MainActivity.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    QuickTimerActivity.this.startActivity(myIntent);
    finish();

This is what need to be done.

Techfist
  • 4,314
  • 6
  • 22
  • 32