Android remove Activity from back stack

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.


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);

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 .


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>

Tags:

Stack

Android