DarkLeafyGreen
DarkLeafyGreen

Reputation: 70406

Android intent to restart app

I have 2 activities LoginActivity (singleTask) and MainActivity (singleTop). The entry point of the app is the login activity. It checks if user credentials are available and then starts the main activity. After that the LoginActivity calls finish. This way I prevent the user from going back to login.

startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();

So the LoginActivity is cleared from the back stack. However I just discovered a disadvantage of calling finish:

When the users clicks on a notification I want to start the LoginActivity:

Intent intent = new Intent(this, LoginActivity.class);
mBuilder.setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));

But the LoginActivity is not on the stack anymore (it was finished), so android will start a new instance of it. So it happens that there are two instances of the whole app. History:

(1) Login -> Main
(2) Main -> (Click on notification) -> Login -> Main
(3) Main -> Main -> (go back)
(4) Main

Since the LoginActivity is the entry point of my app, I cannot route the notification click to the MainActivity. Instead I would like the app to completely restart. Is this possible? Like:

(2) Main -> (Click on notification) -> Login -> Main
(3) Main

Upvotes: 1

Views: 248

Answers (2)

Ozgur
Ozgur

Reputation: 3766

In LoginActivity, after login process, you are calling finish() to destroy that activity.

But when user click to a notification that created from your MainActivity(switched from LoginActivity), you are redirecting again to LoginActivity that already logged user. Why don't you use a separate activity for it?

Also, If I were you, I would do this in authorization process.

App opened -> Main -> onCreate -> isNotLoggedIn -> switchToLoginActivity

Upvotes: 2

lopez.mikhael
lopez.mikhael

Reputation: 10061

I think you need to start your application in (1)Main, do the checking at startup and then spend (2)Login. Indeed, 99% of the time the person is authenticated and will not be directed to Login.

Your multiple instance problem and another problem regardless of your choice (Main/Login or Login/Main). You just don't show notifications when your application is active, but show an internal message like Dialog, Toast, Snack...

(Sorry for my english, I'm french).

Upvotes: 1

Related Questions