16

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.

jlim
  • 801
  • 2
  • 13
  • 21
  • Your manifest looks fine; it should work as you expect. Any chance you're only seeing this under the debugger, like this virtually identical question? http://stackoverflow.com/questions/2061143/android-keep-tasks-activity-stack-after-restart-from-home/2061447 – Christopher Orr Jan 14 '10 at 00:58
  • 5
    i figured it out just as i got your answer, it wasn't eclipse debug related but rather it was related to the run configuration: i had the launch action set to: Launch Default Activity under the Android tab. I changed this to "Do Nothing" and it did the trick. can't believe i took so long to realize this! thanks – jlim Jan 14 '10 at 01:25
  • 4
    You should add an answer with your answer. – Kenny Evitt Mar 30 '10 at 19:05

3 Answers3

1

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.

Ivan
  • 1,735
  • 1
  • 17
  • 26
0

Try using one of these in your manifest :

 <activity android:launchMode=["multiple" | "singleTop" |
                              "singleTask" | "singleInstance"] ...
Vidar Vestnes
  • 42,644
  • 28
  • 86
  • 100
0

Are onResume() and onPause implemented properly?

protected void onResume(){
    super.onResume();
}

protected void onPause() {
    super.onPause();
}
Robert Foss
  • 2,497
  • 4
  • 19
  • 18