Skip to content Skip to sidebar Skip to footer

How To Clear Stack Back To Root Activity When User Leaves Application?

I have an application with 2 activities, LogonAct and MainAct. LogonAct is a logon activity which I want to force the user to go through each time they return to the application. I

Solution 1:

You can do following: 1. set clearTaskOnLaunch = "true" in AndroidManifest, in declaration of main activity 2. in activity that must close:

@OverridepublicvoidonBackPressed(){
    moveTaskToBack(true);
}

so if user presses back - it comes back to homescreen if user launches aplication again - task stack clears and he comes to root main activity

Solution 2:

The docs for android:clearTaskOnLaunch mention that this attribute applies "whenever [the Activity] is re-launched from the home screen".

However, in your case you're pressing the Home button to return to the Home screen, rather than pressing the Back button. This means your application isn't actually relaunched because the MainAct was not "finished". That only happens when you press Back (or if Android kills the task to save resources etc.).

As you only have two activities in your application, you could set the android:noHistory attribute on MainAct, thus ensuring that users can never return to it and must pass through the LogonAct.

As an aside, it seems a bit annoying to force users to re-login every time they navigate away from the app (for example when they receive a phone call). You could retain a session token with timeout in your app's persistent storage, or hold a network connection open using a service if that's how your app works — but of course that's up to you and your requirements. :)

Post a Comment for "How To Clear Stack Back To Root Activity When User Leaves Application?"