Note that there are some explanatory texts on larger screens.

plurals
  1. POOn logout, clear Activity history stack, preventing "back" button from opening logged-in-only Activities
    text
    copied!<p>All activities in my application require a user to be logged-in to view. Users can log out from almost any activity. This is a requirement of the application. At any point if the user logs-out, I want to send the user to the Login <code>Activity</code>. At this point I want this activity to be at the bottom of the history stack so that pressing the "back" button returns the user to Android's home screen.</p> <p>I've seen this question asked a few different places, all answered with similar answers (that I outline here), but I want to pose it here to collect feedback.</p> <p>I've tried opening the Login activity by setting its <code>Intent</code> flags to <code>FLAG_ACTIVITY_CLEAR_TOP</code> which seems to do as is outlined in the documentation, but does not achieve my goal of placing the Login activity at the bottom of the history stack, and preventing the user from navigating back to previously-seen logged-in activities. I also tried using <code>android:launchMode="singleTop"</code> for the Login activity in the manifest, but this does not accomplish my goal either (and seems to have no effect anyway).</p> <p>I believe I need to either clear the history stack, or finish all previously- opened activities.</p> <p>One option is to have each activity's <code>onCreate</code> check logged-in status, and <code>finish()</code> if not logged-in. I do not like this option, as the back button will still be available for use, navigating back as activities close themselves.</p> <p>The next option is to maintain a <code>LinkedList</code> of references to all open activities that is statically accessible from everywhere (perhaps using weak references). On logout I will access this list and iterate over all previously-opened activities, invoking <code>finish()</code> on each one. I'll probably begin implementing this method soon.</p> <p>I'd rather use some <code>Intent</code> flag trickery to accomplish this, however. I'd be beyond happy to find that I can fulfill my application's requirements without having to use either of the two methods that I've outlined above.</p> <p>Is there a way to accomplish this by using <code>Intent</code> or manifest settings, or is my second option, maintaining a <code>LinkedList</code> of opened activities the best option? Or is there another option that I'm completely overlooking?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload