3

I have OnBoard Activity and Login Activity. In LoginActivity after successful login I am trying to clear onboard activity using below code:

startActivity(new Intent(context, HomeActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK));

I've even tried Intent.FLAG_ACTIVITY_NO_HISTORY but it's also not working. So what should I do?

barbsan
  • 3,418
  • 11
  • 21
  • 28
Kalpana Aneyrao
  • 100
  • 1
  • 11

4 Answers4

0

Try below code:

    Intent intent = new Intent(context, HomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
Viraj Patel
  • 2,113
  • 16
  • 23
0

I would say go with the finishAffinity() before moving to onboard activity. There is no need to set any flags. Because this method kills all activities in the stack and current activity as well

Lakhwinder Singh
  • 6,799
  • 4
  • 25
  • 42
0

There is no need to use FLAGS. Even though you want to understand, Check this

There are two ways to finish Current Activity (LoginActivity in your case) and move to Next Activity (OnBoard in your case).:

  1. Call finish() before starting Second Activity. It will destroy current activity.
  2. Call finishAffinity(); before starting Second Activity. It will destroy stack of all previous activity.

Hope it will helps you.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
0

I am assuming that the app starts with OnboardActivity and that starts LoginActivity and then you want to clear them both and launch HomeActivity?

If that is the case, I would do it this way:

  • OnboardActivity launches LoginActivity using startActivityForResult().
  • LoginActivity returns a result that indicates if the login was successful or not and calls finish(). LoginActivity is no longer in the task.
  • OnboardActivity checks the result in OnActivityResult() and, if the login was successful, launches HomeActivity (no flags needed) and calls finish() on itself.
  • At this point, both LoginActivity and OnboardActivity are gone, and HomeActivity is the only Activity in the task.
David Wasser
  • 93,459
  • 16
  • 209
  • 274