1

i have an application that has "login" and "logoff" actions. after the user logs in, there could ne an arbitrary number of activities left on the stack at the point when they log off.

when the user logs off, i want all application state to be reset. i clear my disk caches and preferences, but there's still state left in running (paused) activities. in fact, when the user logs in again, i find that the old activities from the previous login session are still there with their old state (i understand why this is of course). how can i cause the entire app to reset / close / restart?

i've seen a few posts about using startActivityForResult(), onActivityResult() to bubble up a "logoff" result code causing each activity to close itself in turn. this looks painful and i'm hoping there's a better way.

EDIT: i've tried starting the "log in" activity with various flags to no avail,

loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • possible duplicate of [On logout, clear Activity history stack, preventing "back" button from opening logged-in-only Activites](http://stackoverflow.com/questions/3007998/on-logout-clear-activity-history-stack-preventing-back-button-from-opening-l) – Jeffrey Blattman Jun 26 '12 at 00:56

1 Answers1

1

on your manifest you can put the following attribute to your activity:

android:clearTaskOnLaunch="true"

so you would end up with something like:

<activity android:name=".ui.Activity" android:clearTaskOnLaunch="true" android:theme="@style/MyTheme">
                <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
Raykud
  • 2,488
  • 3
  • 21
  • 41
  • that only works when the activity is launched from the home screen. in my case, when the user selects "log off", i want to return to the initial "log in" activity. – Jeffrey Blattman Jun 26 '12 at 00:34
  • 1
    well considering the flags that you have tried and are the actual ones that should work. But then I got a question left, are you trying it on a real device or emulator? – Raykud Jun 26 '12 at 00:42
  • if so then you could have the same issues as here: http://stackoverflow.com/a/5794572/1084764 – Raykud Jun 26 '12 at 00:43