Pergunta

For simply, I write an app with Splash, Home, Child1 and Child2 with following flow: Splash -> Home -> Child1 -> Child2. And we can back from Child2 to Child1, from Child1 to Home, and back from Home to exit app.

It sounds simple. I can navigate between these screens correctly.

Problem when I install app via OTA. After enter Splash -> Home -> Child1 -> Child2. Staying Child2, I press HOME button to minimize app, and re-enter app by choose app icon. But when re-entering app, it doesn't resume at Child2 but start from Splash -> Home. I cannot understand what happens here. It should resume at Child2 instead.

More surprised thing is, at Home screen, when I press BACK button to exist app, now Child2 is resumed (o_O). And from Child2, now I can back to Child1, and to Home, and back again to exist app.

All these issues happen when I install app via OTA but app works normally when I install by cable.

Can anyone tell me what happens in my situation? Any help is appreciated

Many thanks.

P/S: I get this bug when testing on Nexus 1 (Android 2.2) and Nexus S(Android 2.3.x)

Edit: even CNN app for Android still have this issue... Is it belong to OS?

Foi útil?

Solução

This has to do with task affinity I guess. And it's not just the CNN app, all apps behave like that because some developers (like me!) don't understand how the task concept works. I won't lie to you, even I don't understand this all the time.

I believe this happens because, when you launch your app from the Market, your app becomes part of the Market task. Therefore, you have an instance that "belongs" to the Market task. You can navigate normally from it. All the problems happen when you press Home because...

If you start the activity from the Launcher, you start a new task for the app, giving you 2 "instances" of the app, one belonging to the previous Market task, the other belonging to your own app (where you are the root of your own task).

On this new task, you can navigate back, and eventually you will reach the previous task, which has another instance of your app. You will reach the point where you left off (considering regular launch mode for the activities). Sometimes, this is odd because we normally think that one app has only one instance, when it's not how it works, nor it's how it should work. I loosely compare that with launching twice an app in Windows having two Windows open. In Android, this analogy is similar (at least to me).

This is all fine when you start your app from another app, when the task concept feels natural.

However, in this case it becomes weird because when we launch the activity from the Market, we expect the instance to be the same that will be in the Launcher (when launched from there). We naturally don't expect that the Market, used to download the app, will own an app that we installed, and that we (obviously) want to use separately from the Market app (I don't think a Market should own a task when using the "Open" button).

To solve that, I believe you could fiddle with allowTaskReparenting. However, in case it's really that which is causing this issue, I don't recommend it because this could break other legitimate uses for the task system.

Finally, this is how I believe tasks work, and that's my guess to where this problem comes from. I could be wrong, so please forgive me if this answer becomes useless later (if you find another cause that answers this).

Good luck!

Outras dicas

Looking at your comment you kill your activities as soon as you start a new one..

E.g. if you do:

startActivity(intent);
finish(); // THIS WILL KILL THE ACTIVITY -> MAKING IT UNREACHABLE

The activity that launches the new activity is being killed and cannot be reached using the back button.

Remove the

finish(); // REMOVE THIS TO KEEP IT ON THE ACTIVITY STACK

after you call startActivity(intent), and all your activities should stay in memory. Hence, they will be reachable when you go back. NOTE: for the Splash, you should indeed call finish(), since you don't want to go back to the Splash activity after a succesfull launch!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top