Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Activity killed by System if No Longer Visible in Android

I have There Activities(A,B,C) within my Application.When i start the Application Activity A:

  A:onCreate()
  A:onStart()
  A:onResume()

Using intent i am calling Second Activity(A -> B):

  A:onPause()
  B:onCreate()
  B:onStart()
  B:onResume()
  A:onStop()

Then I click the "Home" button So the App goes to background:Now

  B:onPause()
  B:onStop()

After 1 or 2 hour later Again i go to home page within my device and Click the App icon it runs like:

 B:onDestroy()
 A:onRestart()
 A:onStart()
 A:onResume()

But i need to go which one Activity i quit like this,

B:onRestart()
B:onStart()
B:onResume()

I have read some articles it says like that activity killed by the system because of no longer visible.Is there any possible to fix my issue...

Thanks in advance...

like image 405
Srinivasan Avatar asked Jul 26 '12 08:07

Srinivasan


People also ask

What happens when an activity is destroyed android?

Android destroys the activity, and then recreates it. The onCreate() method gets called, and the Bundle gets passed to it.

Which method is called when an activity is no longer visible to the user and it will not be needed for a while?

onStop() When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback. This may occur, for example, when a newly launched activity covers the entire screen.

Which method is called when Android system kills the activity due to memory issue?

A cached process is one that is not currently needed, so the system is free to kill it as desired when resources like memory are needed elsewhere.

What happens when an app is killed?

It depends on the version of the platform. Prior to 2.2, third party applications like ATK could use an API that did the same thing as the "Force Stop" button in manage apps -- this kills all app processes, removes all tasks/activities, unregisters all alarms, removes all notifications, stops all services, etc.


2 Answers

You may be confusing two different things here:

  1. Android does not kill an activity if it needs memory. What it does is that it kills the whole process that the activity is running in. In general that means that Android kills all of your activities in this situation. However, it remembers the activity stack and when the user returns to the application, Android will create a new process and then recreate each activity (in turn, as needed). It starts by recreating the activity that was on the top of the activity stack (ie: where the user left the application).

  2. Android assumes that if the user leaves a task for a long period of time (I think this is something like 30 minutes) then he is no longer interested in that task and there is no point in remembering where the user was in the activity stack of that task because he probably doesn't care anymore. In this case, what happens is that when the user returns to the task (or restarts the application that was on the top of the activity stack in that task) Android simply clears the task back to the root activity. This has the effect that it looks like the application is starting all over again. This is the desired (and documented behaviour).

What you want to do is prevent Android from clearing the task in situarion #2. You do it by adding

    android:alwaysRetainTaskState="true"

to the <activity> tag of the root activity (ie: the activity that starts your application, the one with ACTION_MAIN and CATEGORY_LAUNCHER).

like image 69
David Wasser Avatar answered Nov 14 '22 21:11

David Wasser


I don't believe this is something you can surely control. If your activity is in background for a lot of time and meanwhile other applications need memory, the system will kill your activity to free memory.

like image 27
Andy Res Avatar answered Nov 14 '22 23:11

Andy Res