I have an application that has an initial activity (A - also the one specified in the launcher) from which it allows the user to launch another activity (B). I am saving the state of Activity B (onSaveInstanceState() and restoring it in onCreate()).
On some phones (N1 and Motrola Milestone as of now but not the Galaxy S) after launching B if the user presses the home button (i.e. app goes to background) and then immediately re-launches the app, they are shown activity A again (not B). But if they launch it from the 'Recent' apps (Home button long press) they are taken to the activity B.
From what I understand of the Android docs, unless the task has been killed (doesnt seem like the case here as nothing else is done except re-launch the activity from the app screen) we should see activity B restored on launch (as that is on top of the stack). So I just can't seem to figure out what the heck is wrong here.
BTW orientation changes during app usage works fine (everything saves and restores fine). This weird problem has been bugging me for some time now and by trial and error I discovered that when I disabled orientation for that activity and then trying the re-launching got B to show as expected.
So has anyone else faced something like this and if so how can this be resolved?
I am looking into saving the running activity in SharedPreferences (as mentioned in this post) and they restoring it from that in activity A, however would like to know if I am missing something here. Shouldn't having B restored on re-launch be the default behavior?
When Home button is pressed, onStop method is called in your activity. So what you may do is to add finish(); in onStop method to destroy your activity. Eventually onDestroy method will be raised to confirm that your activity is finished.
Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background.
A user-readable label for the activity. The label is displayed on-screen when the activity must be represented to the user. It's often displayed along with the activity icon.
Try adding this to your activity inside the manifest file:
android:launchMode="singleTask"
This resolved the issue in my app .... if I understand your problem correctly.
Use android:launchMode="singleInstance" instead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With