Clear the Backstack within an activity without usi

2019-01-12 12:28发布

问题:

I want to clear the back stack within an Activity but not by startActivity() and using FLAG. e.g. when I launch an application from the application icon then the application main activity starts but there is some thing in the back stack like the launcher activity because when we touch the minimized app tab the launcher is visible . I want to remove the launcher application from the minimized applications.

回答1:

@SorryForMyEnglish's answer is right. You just cannot to implement it. By using android:noHistory="boolean" attribute, see my concept maps below:

Because of ActivityC and ActivityD (last activities) has a true value, so they cannot back to MainActivity, but they can back to ActivityA and ActivityB. Also, ActivityA and ActivityB can back to MainActivity. And the backstack is completely cleared without using this startActivity(intent) to open your next Activity (so you will need FLAG):

Intent intent = new Intent(CurrentActivity.this, NextActivityToBeOpened.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

After you declared the value in manifest, you just need to call this startActivity(intent) to open the next Activity (no FLAG is needed):

startActivity(new Intent(CurrentActivity.this, NextActivityToBeOpened.class));

Is it very simple, right?

Remember:

  • Set your last Activity with android:noHistory="true" in manifest.
  • Another Activity must be set with false value.
  • Apply this attribute for all of your Activity.

As additional, here is how to use it inside your manifest:

<activity android:name=".MyActivity" android:noHistory="true" />


回答2:

use android:noHistory="true" attribute in Manifest for your Activity

<activity android:name="StartActivity"
            android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>