Viking
Viking

Reputation: 127

Not able to preserve the current activity when the application is re-started

I have 3 Activities:Activity A,B and C Activity A is my login activity where user logs in to his account, Activity B is the dashboard and Activity C is has ListView

On Android Phone: When im on Activity C i press Home button on my phone it takes me to home screen of phone and than when i try to start the app again it takes me to Activity A instead of Activity C where i left initially.

On Emulator: When im on Activity C i press Home button on my phone and than when i try to start the app again it takes me to Activity C.

Also i have setup onResume on each activity just to check if phone has access to internet when the activity is resumed. Now the problem is that i don't understand why im not getting the same behavior on phone as im getting on emulator.

I want app to start from the Activity C where i left it.I want the same behavior as im getting on Emulator.

Also no errors or crash is happening on emulator.So its hard for me to track down the problem.

Please guide me on what should i do or please tell me what might be the problem.

Thank You!!

Upvotes: 0

Views: 123

Answers (3)

AlexN
AlexN

Reputation: 2554

Concerning you are not using Eclipse to launch the app - I would like to introduce another solution.

Please take a look at the following key of Activity tag in Manifest: android:alwaysRetainTaskState http://developer.android.com/guide/topics/manifest/activity-element.html#always

According to documentation, it's default value is false. If set to true in the task-root activity (Launcher in your case, I think) - it will tell the system to always keep the state of the task and do not reset it. So even when the process will be killed - after relaunch you should appear in the latest task state.

However, be careful, because this approach will be ruined if

  1. You will specify it not for root of the task.
  2. If you use Task-manipulating flags for your intents inside application. For example FLAG_ACTIVITY_CLEAR_TASK/FLAG_ACTIVITY_CLEAR_TOP/etc

Good luck

Upvotes: 1

Sayyam
Sayyam

Reputation: 959

Well, both of the behaviors are normal. And here is what is happening: When you press the home button from "Activity C" OS takes you to the android home screen and then its up to the OS to decide whether to destroy the Activity or keep it (it depends on the resources OS need).

Well on emulator there might be no or very few other activities running so the OS do not destroy the "Activity C" and when you re launch the application it takes you to the "Activity C" which still is there.

In contrast to emulator your device must be running a plenty of other applications and services and it takes resources to keep them running smoothly, so when you exit an Activity OS destroys that Activity and claims the resources which were given to that application. So when you start the application it recreates or relaunches the application from start which in your case is "Activity A".

Thus, its pretty much normal behavior and you should be worrying about it.

For further details you can read this.

Upvotes: 1

AlexN
AlexN

Reputation: 2554

Depending on what Eclipse ADT plugin are you using, the behavior on phone may be wrong just because you are starting application from eclipse. You can try to "adb install your_app.apk" and start it manually from screen, it can solve the problem.

In general, the behavior that you'd described is a designed one, so if not to interrupt(by intent flags and manifest statements) - system should behave just as you wished, restoring the last activity and it's state.

Good luck

Upvotes: 0

Related Questions