开发者

Restore android app stack from background

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.


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.


Try using one of these in your manifest :

 <activity android:launchMode=["multiple" | "singleTop" |
                              "singleTask" | "singleInstance"] ...


Are onResume() and onPause implemented properly?

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

protected void onPause() {
    super.onPause();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜