1

In my app I have the following Activity flow: LoginActivity > DashboardActivity > (if user opts to change their password) > ChangePasswordActivity

If the user successfully changes their password, I'd like to send them back to the very first Activity which is LoginActivity. I would like to pop DashboardActivity in the process to prevent any unwanted lingering Activities.

Is it correct to use FLAG_ACTIVITY_CLEAR_TOP to start a new LoginActivity in this case? I'd like to know which flags are appropriate in this scenario.

For what it's worth, my DashboardActivity has a launchMode set to singleTop in my AndroidManifest.

Quick learner
  • 10,632
  • 4
  • 45
  • 55
fishhau
  • 459
  • 1
  • 3
  • 15
  • This answer explains it. In short if you already have login screen in the backstack and want to lauch it without resetting the stack then clear_top would be ideal. Still read this https://stackoverflow.com/a/23718678/2936153 – Rohit5k2 Jun 04 '19 at 05:57
  • https://stackoverflow.com/questions/7385443/flag-activity-clear-top-in-android?noredirect=1&lq=1 – Rohit5k2 Jun 04 '19 at 05:58
  • If that was my task, I'd check myself. Isn't it more exciting to discover it yourself? – Vladyslav Matviienko Jun 04 '19 at 06:02
  • you can use finishAffinity in place of finish but yes it will create new instance of Login Activity as well. check this https://developer.android.com/reference/android/app/Activity.html#finishAffinity() – Jishant Jun 04 '19 at 07:00

2 Answers2

2

If you want to just clear the previous activities from stack and launch login activity just do this

in manifest file your login activity should be like this

<activity
    android:name=".LoginActivity"
    android:screenOrientation="portrait"/>

After changing password launch the login activity with following tags

Intent intent = new Intent(ChangePassword.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();

By setting flag to Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK all the previous activities will be cleared from stack.

for more info check this https://developer.android.com/reference/android/content/Intent From the official documentation - FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

Quick learner
  • 10,632
  • 4
  • 45
  • 55
1

LoginActivity > DashboardActivity > (if user opts to change their password) > ChangePasswordActivity

  • In order to clear task and intent to LoginActivty you should use

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

Is it correct to use FLAG_ACTIVITY_CLEAR_TOP to start a new LoginActivity in this case? I'd like to know which flags are appropriate in this

  • FLAG_ACTIVITY_CLEAR_TOP will clear everything from the stack and make the new activity as root task. In your case, there is no previous activity for LoginActivity it will also work but not advisable.

For what it's worth, my DashboardActivity has a launchMode set to singleTop in my AndroidManifest.

Deejayen
  • 23
  • 1
  • 8