Android: Clear the back stack

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

    You can use this example to call your Activity A from Activity C

    Intent loout = new Intent(context, LoginActivity.class); loout.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(loout);

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

    Starting in API 16 (Jelly Bean), you can just call finishAffinity().

    Now you can also call ActivityCompat.finishAffinity(Activity activity) with the compatibility library.

    Be sure to set taskAffinity in the manifest to a package name unique to that group of activities.

    See for more info:
    http://developer.android.com/reference/android/support/v4/app/ActivityCompat.html#finishAffinity%28android.app.Activity%29

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

    In kotlin it is almost same like java. Only | symbol is replaced by or text. So, it is written like-

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
    
    0 讨论(0)
  • 2020-11-22 07:50

    Use finishAffinity(); in task C when starting task A to clear backstack activities.

    0 讨论(0)
  • 2020-11-22 07:51
    1. Add android:launchMode="singleTop" to the activity element in your manifest for Activity A
    2. Then use intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) and intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) when starting Activity A

    This means that when Activity A is launched, all tasks on top of it are cleared so that A is top. A new back stack is created with A at the root, and using singleTop ensures you only ever launch A once (since A is now on top due to ..._CLEAR_TOP).

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

    Intent.FLAG_ACTIVITY_CLEAR_TOP will not work in this case. Please use (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)

    For more detail please check out this documentation.

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