Android: Clear the back stack

前端 未结 30 1861
被撕碎了的回忆
被撕碎了的回忆 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:37

    If your application has minimum sdk version 16 then you can use finishAffinity()

    Finish this activity as well as all activities immediately below it in the current task that have the same affinity.

    This is work for me In Top Payment screen remove all back-stack activits,

     @Override
    public void onBackPressed() {
             finishAffinity();
            startActivity(new Intent(PaymentDoneActivity.this,Home.class));
        } 
    

    http://developer.android.com/reference/android/app/Activity.html#finishAffinity%28%29

    0 讨论(0)
  • 2020-11-22 07:37
    Use finishAffinity() to clear all backstack with existing one.
    
    Suppose, Activities A, B and C are in stack, and finishAffinity(); is called in Activity C, 
    
        - Activity B will be finished / removing from stack.
        - Activity A will be finished / removing from stack.
        - Activity C will finished / removing from stack.
    
    0 讨论(0)
  • 2020-11-22 07:37

    I tried all solutions and none worked individually for me. My Solution is :

    Declare Activity A as SingleTop by using [android:launchMode="singleTop"] in Android manifest.

    Now add the following flags while launching A from anywhere. It will clear the stack.

    Intent in = new Intent(mContext, A.class);
    in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
    startActivity(in);
    finish();
    
    0 讨论(0)
  • 2020-11-22 07:37

    In manifest

    android:launchMode="singleTask"
    

    and

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-22 07:38

    It sounds to me like you need to start Activity C from Activity B by using startActivityForResult(). When you click a button in Activity C, call setResult(RESULT_OK) and finish() so Activity C is ended. In Activity B, you could have the onActivityResult() respond by also calling finish() on itself, and you'd then be taken back to Activity A.

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

    In addition to FLAG_ACTIVITY_CLEAR_TOP, you may try adding Intent.FLAG_ACTIVITY_SINGLE_TOP as well:

    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    0 讨论(0)
提交回复
热议问题