Clear all activities in a task?

后端 未结 7 1509
有刺的猬
有刺的猬 2020-11-30 01:44

I have a splash screen activity, then a login activity. My history stack looks like:

SplashActivity
LoginActivity

when the user successfull

相关标签:
7条回答
  • 2020-11-30 02:03

    finish() removes the activity from the stack. So, if you start LoginActivity and call finish() on SplashActivity, and then you do exact the same to start WelcomeActivity you will get the desired behaviour. No need to use extra flags.

    0 讨论(0)
  • 2020-11-30 02:10

    Intent.FLAG_ACTIVITY_NO_HISTORY can work in your case too if you do not want the activity on the history stack.

    0 讨论(0)
  • 2020-11-30 02:10
    • In case that all of three activity is involved in the same app(same taskAffinity), you can pick either 1,2 or 3 below. otherwise you should pick 1,2 below.

      1. If you don't want to return back SplashActivity from LoginActivity, you can define activity attribute noHistory in AndroidManifest.xml or you can set FLAG_ACTIVITY_NO_HISTORY into the intent to launch SplashActivity. if SplashActivity is started from Launcher, you should pick way to set activity attribute noHistory.

      2. If you don't want to return back LoginActivity from WelcomeActivity, you can use either activity attribute noHistory or FLAG_ACTIVITY_NO_HISTORY like number 1 above.

      3. If you want to clear back stack on specific situation, you can use FLAG_ACTIVITY_CLEAR_TASK in conjunction with FLAG_ACTIVITY_NEW_TASK(FLAG_ACTIVITY_CLEAR_TASK always must be used in conjunction with FLAG_ACTIVITY_NEW_TASK). But, if the activity being started is involved in other app(i.e different taskAffinity), the task will be launched other task after the task is cleared, not current task. so make sure that the activity being launched is involved in the same app(taskAffinity).

    0 讨论(0)
  • 2020-11-30 02:15

    Yes that should work fine. You could use:

    • FLAG_ACTIVITY_CLEAR_TOP
    • FLAG_ACTIVITY_SINGLE_TOP
    • FLAG_ACTIVITY_CLEAR_TASK
    • FLAG_ACTIVITY_NEW_TASK

    which ensures that if an instance is already running and is not top then anything on top of it will be cleared and it will be used, instead of starting a new instance (this useful once you've gone Welcome activity -> Activity A and then you want to get back to Welcome from A, but the extra flags shouldn't affect your case above).

    0 讨论(0)
  • 2020-11-30 02:22

    Use android:noHistory="true" on the splash activity in the manifest file.

    <activity 
    android:name=".activity.SplashActivity" 
    android:theme="@style/theme_noActionBar" 
    android:noHistory="true">
    
    0 讨论(0)
  • 2020-11-30 02:28
    Intent intent = new Intent(this, NextActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                        IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
    
    0 讨论(0)
提交回复
热议问题