Android Home button not resuming last Activity
I am trying to solve an issue sort of similar to what was done here (link text)
Except in my case where Activity A, started Activity B, started Activity C, when the user resumes the app after pressing the home button - I want C, the last Activity, to be displayed. Instead what is happening is I am back to A.
Also on a side note I have discovered that even when I exit the application, if I hold down the Home button, my app is still listed as an active app even though in the LogCat I can see that it exited OK.
Also, in case it matters, I did recently add android:launchMode ="singleTask"
in order to make some notification logic work correctly. I was seeing the Home button behavior before that though.
I really appreciate any ideas you might have.
[edit - adding some code snippets]
<activity
android:name =".connection.ActivityA"
android:label ="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme ="@android:style/Theme.NoTitleBar"
android:launchMode ="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name ="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name =".screens.AvtivityB"
android:label ="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme ="@android:style/Theme.NoTitleBar"
android:launchMode ="singleTask"
android:screenOrientation="portrait" >
</activity>
<activity android:name =".screens.ActivityC"
android:label ="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme ="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait" >
</activity>
Then here is how I kick off each Activity: In Activity A:
private void startActivityB() { Intent activityBIntent = new Intent(this, ActivityB.class); this.startActivityForResult(activityBIntent , ACTIVITYB_TAG); }
Activity B actually has several Activities that it chooses from (it has a list of Intents) to display based on input it receives but launches them all the same way as Activ开发者_高级运维ty A launched B. Once the first Activity is displayed the user swipe the screen to navigate to the left or right screen. When the user swipes left or right, the current activity puts info in its return Intent that Activity B uses to display the next Activity. Also since logic outside of the user's control could prompt a change in the screen displayed, each Activty calls finish on itself in onStop to avoid back-button issues.
When you long press home it is showing a list of recently active applications, not applications that are currently running. So you should still see your app in the list, even if it is closed.
As the docs note:
The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.
I'd imagine that your using singleTask is affecting the history stack, and thus preventing Activity C from being remembered. If you remove that from Activity C, does it resolve the problem?
Otherwise, I'd look at what you are doing in onPause/onDestroy etc for Activity C. Are you doing something that would cause it to finish or close?
Do you get this behavior only when running from Eclipse?
Check your "Run Configurations". There is a "Launch Action" that can be set to "Do Nothing".
Matt
I'm not sure this is what it happening to you, but the answer below solved my problem and I wanted the same behavior as you - that is, getting to the same activity when re-opening after home has been pushed (though in my case, I did actually want the first activity, just not a new one):
Save activity state in android when home button pressed
Now I realize you want to keep the activity stack intact and not go to the first activity like me. But if it is the same problem, then it is not from the fact that the activity stack is torn down, but because a new one (with the opening activity) is added when re-opening after home has been pressed on first install. I hope this is fairly legible, even though I (clearly) found it difficult to explain.
Edit: This is better described here and here.
精彩评论