Pergunta

I have an application with multiple activities. The first one is a login/startup activity followed by a few that change during the lifetime of the application (and its "logged in" connection).

If the user presses the home button, it returns to the Launcher. All well and good. And if the user goes to "Recent Apps" and selects my app, it returns right where it left off (the desired behavior). If the user instead goes to my icon in the launcher and starts the application again, I get one of two behaviors. 1. The activity is returned exactly where it was before just as in the case when I choose it from the recent apps. 2. The activity is taken to the initial login screen. Not what I was expecting. This seems to not be the case on most of the devices and versions of Android, but it seems to happen on some device every time. I have not been able to pin it to 4.0.4 or 4.1 or 4.2 - we see this behavior on all 3 versions on some phones.

When I look at the activity stack (using dumpsys) on these systems when I have moved to the launcher, it appears that they are the same (I see my most recent on the stack, just under the launcher and I also see my application stack with my most recent on top). But when I click on the app icon in the launcher, on most devices, I see it return and the activity stack shows my most recent on top. But on these other few devices, I see the login activity on top of my most recent. If I click on the "back" button while in the login activity, I happily go back to where I left off.

To illustrate, on some devices after I hit the home and then click on my app icon, I see the following stack:

"ConnectActivity2"
"ConnectActivity1"
"LoginActivity"

And on these others I see:

"Login Activity"
"Connect Activity2"
"Connect Activity1"
"Login Activity"

Is there an option that controls this behavior on the device/operating system? Or is there something in my app I need to do when transitioning to "home" that dictates how it returns to my app (and if so, why is only required sometimes)?

Foi útil?

Solução

I don't know such an option and I think it does not exist because the main app activity that should be started on app start is specified in manifest file. However, in my app, where I had to do the same as you, I created activity named SwitchActivity.

Its purposes are the following:

  • show splash screen with big logo or whatever
  • while splash is showing, check if you have already logged in user
  • if yes - then forward to whatever activity you need, meaning that you close splash and create new root activity with own stack so that when user taps back button on root activity the app goes background
  • if not - forward to login.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top