Android remove Activity from back stack

85,640

Solution 1

FLAG_ACTIVITY_CLEAR_TOP clears your Activity stack , you can use the code below:

Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Remember that this flag clears just Intermediate Activities , for example if you have A,B,C in your Back Stack then going from C Activity to D with this flag this does not clear Back Stack and the Stack would be A,B,C,D but if you go from Activity D to Activity A with this flag , B,C,D Activities will pop up from the stack and you will have just A in the Back Stack .

Solution 2

simple solution for API >= 15 to API 23 user activity name in intent.

 Intent nextScreen = new Intent(currentActivity.this, MainActivity.class);
 nextScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
 startActivity(nextScreen);
 ActivityCompat.finishAffinity(currentActivity.this);

Solution 3

To remove activity from back stack inside manifest add android:noHistory="true" to your activity inside the manifest file.

See sample below.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.activity"
      android:versionCode="1"
      android:versionName="1.0">
 <application android:name="MyApp" android:label="My Application">
    <activity android:name=".LoginActivity" 
      android:noHistory="true"> //add this line to your activity inside manifest
     <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
   </activity>
 </application>
</manifest>

Solution 4

I would suggest that you use startActivityForResult(), instead of simply startActivity(), when you launch the EditDegreePlan-Activity, as described in the Android tutorials.

In the EditDegreePlan-Activity you then call

setResult(RESULT_OK);
finish();

If you don't expect any data from the EditDegreePlan-Activity, then you don't necessarily have to implement the onActivityResult.

Solution 5

It seems, that you will get the desired behavior if you do not specify any flags at all. The back button would just do the right thing. To get an activity closed from within your code use the finish() method it has the same effect as the user pressing the back button. So you will automatically arrive at DegreePlan when you finish the EditDegreePlan, no need to call any Intents either.

Share:
85,640

Related videos on Youtube

Emrys90
Author by

Emrys90

I'm working on my basics in college at the moment, hoping to become a computer programmer. I just recently started learning android development, and new to Java as well. Suffice it to say I'm a newbie. Hopefully with the help of this website and various tutorials I can become a good programmer.

Updated on July 05, 2022

Comments

  • Emrys90
    Emrys90 almost 2 years

    Okay so I'm kind of stumped on what to do with this. So I have the MainActivity, and from there an Activity can be launched to DegreePlanActivity, and from there another Activity can be launched to EditDegreePlan. I've got EditDegreePlan set to noHistory in the AndroidManifest. The problem is after they save the EditDegreePlan it launches an Activity to DegreePlan. So if the user presses Back they have to press it twice to get to MainActivity again. I want to get rid of that so they only have to press it once. I'm stumped on how to do this though.

    If I set DegreePlanActivity to noHistory then they couldn't press Back to it while in EditDegreePlan.

    I've tried overriding onBackPressed method and launching an intent to MainActivity. The problem then is that they have to press Back multiple times to exit the app then.

    What should I do?

    • Md Abdul Gafur
      Md Abdul Gafur over 11 years
      write finish() after startActivity method
  • Emrys90
    Emrys90 over 11 years
    Thanks! That worked perfectly. I was even able to remove the noHistory from AndroidManifest. Just adding that one line of code to the save method of EditDegreePlanActivity fixed everything.
  • ralphgabb
    ralphgabb over 8 years
    how bout those activities with taskAffinity = ""; ?
  • danguilherme
    danguilherme almost 8 years
    Incredible idea! My use case: a simple login activity (that could lead to a register activity, seamless to the user flow), when done login, open a feature activity. If user hit back, it should show my first activity (the one that triggered login). Resolution: login and register activities are now called with startActivityForResult. When they're done, they're out of the stack. Thanks!
  • Admin
    Admin about 7 years
    Awesome! Thnx for something current.
  • Marek
    Marek about 7 years
    Wow, that is a great feature. I can't believe it works and matches exactly my needs. I have a background async service which scans barcodes. And on specific barcodes it starts some activities. And in some cases I need to remove some started activities from stack. This just works great!
  • Akshatha S R
    Akshatha S R about 6 years
    Thanks a lot, but IntentCompat.FLAG_ACTIVITY_CLEAR_TASK is deprecated, use Intent.FLAG_ACTIVITY_CLEAR_TASK instead
  • Makarand
    Makarand over 4 years
    no matter if you call it before or after startActivity(), there won't be no first activity
  • GreyBeardedGeek
    GreyBeardedGeek over 3 years
    Especially useful for a splash screen - configuration is always better than code
  • iamkdblue
    iamkdblue over 3 years
    Nice explanation!
  • Jemshit Iskenderov
    Jemshit Iskenderov over 2 years
    Even when app goes background, Activity gets destroyed
  • Jemshit Iskenderov
    Jemshit Iskenderov over 2 years
    Nope, finish() can't do it sometimes (startActivity+finish), Activity still lives for 10 more seconds