Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore android app stack from background

Tags:

java

android

Lets say I launch my app from the home screen, navigate through some activities, then I press the home key and do something else in the Gmail app.

After i'm done checking my mail,I press the home key again to leave the Gmail app and click my app's icon at the home screen again to return to it.

When I return to my app, I want it to return to the last activity I was at, NOT start a whole new session. I've been trying to figure this out all day.

My manifest for my first activity is as follows:

 <activity android:name=".Main"
              android:label="@string/app_name"
              android:screenOrientation="portrait"
              android:alwaysRetainTaskState="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

The category attribute LAUNCHER makes my app ALWAYS start at activity Main, so I don't know how to go about restoring the last activity. People have told me to use sharedpreferences to save the last activity and load it on Launch, but I don't think it's intended to be done that way because it isn't very elegant.

like image 781
jlim Avatar asked Jan 14 '10 00:01

jlim


1 Answers

I think its the only way because what happens when you're launching an app is that Launcher application sends intent "android.intent.action.MAIN" And the only activity in your app that responds to this intent is your main activity, thus it gets activated. So the only thing you can do is save somewhere your session and on activity start up if there's already saved session restore the app to the previous state.

like image 200
Ivan Avatar answered Sep 28 '22 23:09

Ivan