Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After Home button press re-launching app shows initial activity not current

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?

like image 437
source.rar Avatar asked Aug 01 '11 20:08

source.rar


People also ask

What happens to activity when home button is pressed?

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.

What happens when Home button is pressed in Android?

Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background.

What is the use of activity tag?

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.


2 Answers

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.

like image 179
Tony Avatar answered Sep 19 '22 13:09

Tony


Use android:launchMode="singleInstance" instead

like image 30
Baschtli Avatar answered Sep 19 '22 13:09

Baschtli