82

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?

Emrys90
  • 1,007
  • 1
  • 10
  • 15

11 Answers11

159

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 .

Arash GM
  • 10,316
  • 6
  • 58
  • 76
  • 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. – Emrys90 Jan 01 '13 at 17:10
  • 1
    how bout those activities with taskAffinity = ""; ? – ralphgabb Aug 11 '15 at 06:54
  • 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! – Marek Apr 27 '17 at 09:50
21

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>
Isaac Sekamatte
  • 5,500
  • 1
  • 34
  • 40
18

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);
Kamal Bunkar
  • 1,354
  • 1
  • 16
  • 20
  • 5
    Thanks a lot, but IntentCompat.FLAG_ACTIVITY_CLEAR_TASK is deprecated, use Intent.FLAG_ACTIVITY_CLEAR_TASK instead – Akshatha S R Apr 19 '18 at 07:48
15

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.

Soana
  • 711
  • 7
  • 15
  • 1
    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! – danguilherme Jul 28 '16 at 01:10
7

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.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • Nope, finish() can't do it sometimes (startActivity+finish), Activity still lives for 10 more seconds – Jemshit Dec 16 '21 at 12:27
7

You can call finish before you start your new activity. This will remove the current activity from the stack, so when you press back button from the next activity, the first activity will not be called from the stack history.

Intent i = new Intent(MainActivity.this, NextActivity.class);
finish();  
startActivity(i);
Mohit Yadav
  • 158
  • 3
  • 11
live-love
  • 48,840
  • 22
  • 240
  • 204
5

Here is your flow:

MainActivity --> DegreePlanActivty --> EditDegreePlan--> DegreePlan --> MainActivty

Override these method inside your "DegreePlan"

public void onBackPressed() {
   super.onBackPressed();
   Intent goToMainActivity = new Intent(getApplicationContext(), MainActivity.class);
   goToMainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Will clear out your activity history stack till now
   startActivity(goToMainActivity);
}
Vladimir Markeev
  • 654
  • 10
  • 25
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
2

use this to clear the stack :

 menuIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Heena Arora
  • 739
  • 8
  • 18
1
Intent intent = new Intent(getContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

startActivity(intent);
adiga
  • 34,372
  • 9
  • 61
  • 83
Luis Muñoz
  • 139
  • 1
  • 4
0

You can add flags as follows and start Activity, try below code

Intent i = new Intent(activity, Payment.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(i);
adiga
  • 34,372
  • 9
  • 61
  • 83
0
This code should help you out: It is in Kotlin
private fun verifyIfUserIsLoggedIn(){
        val uid = FirebaseAuth.getInstance().uid
        if(uid== null){
            val intent = Intent(this, MainActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivity(intent)
        }
    }