Android: Clear the back stack

前端 未结 30 1830
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 06:47

In Android I have some activities, let\'s say A, B, C.

In A, I use this code to open B:

Intent intent = new Intent(this, B.class);
startActivity(inte         


        
相关标签:
30条回答
  • 2020-11-22 07:40

    The given code works correctly. I have tried in the Application Life Cycle sample.

    I haven't got B and C in the back stack after starting activity A with flag, FLAG_ACTIVITY_CLEAR_TOP

    0 讨论(0)
  • 2020-11-22 07:41

    i called activity_name.this.finish() after starting new intent and it worked for me.

    I tried "FLAG_ACTIVITY_CLEAR_TOP" and "FLAG_ACTIVITY_NEW_TASK"
    

    But it won't work for me... I am not suggesting this solution for use but if setting flag won't work for you than you can try this..But still i recommend don't use it

    0 讨论(0)
  • 2020-11-22 07:42

    logout.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); logout.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    0 讨论(0)
  • 2020-11-22 07:43

    I found the answers here a little misleading because the code in the original question seems to work fine for me?

    With A being the root activity, starting it from B or C only with FLAG_ACTIVITY_CLEAR_TOP does remove B and C from the back stack.

    0 讨论(0)
  • 2020-11-22 07:44

    This bothers me for a long time .Finally I worked it out by doing this:

    In fragment,use:

    Intent intent = new Intent(view.getContext(), A.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intent);
    

    In Activity,use(add one more intent flag Intent.FLAG_ACTIVITY_CLEAR_TASK compared to fragment):

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-22 07:45

    As per Wakka in Removing an activity from the history stack...


    Add android:noHistory="true" attribute to your <activity> in the AndroidManifest.xml like this:

        <activity android:name=".MyActivity"
            android:noHistory="true">
        </activity>
    
    0 讨论(0)
提交回复
热议问题