Did back key destroy an activity? - android

I have an activity defined as below:
<activity android:name="com.example.ui.HomeActivity"
android:alwaysRetainTaskState="true"/>
A strange thing is that, when running on emulator, and the back key is pressed, the activity was destroyed (I saw onDestroy() called in log). But when running on my Nexus One phone, and the back key is pressed, the activity is not destroyed (I didn't see onDestroy() called in log).
Could someone tell me why?
Thanks.

When activity is in the background (after pressing Back key) it is always stopped but System can also decide to destroy it (eg. when system resources are low). You can't determine when it will be destroyed.
Activity can also be destroyed by calling finish(). You can determine that by checking isFinishing() status in onPause or onStop callback.
Do not count on onDestroy callback. If system will kill activity it wont be called.

By default, pressing the BACK key finishes (destroys) the current activity and displays the previous activity to the user.
source

Related

Life cycle of Android Activity after pressing Back button

I am little confused between the life cycle of two activities.
Suppose I have Activity A and Activity B.
B is called From A i.e A ----> B.
Now currently B is on the screen and I pressed back button. Here I want know:- is there any memory still available for B(Active) or B's memory is flushed(Inactive).
The following activity call back methods are called, after pressing back button.
onPause()
onStop()
onDestroy()
The activity is destroyed.
And it recreates when launched again. These are the callback methods when it launches again.
onCreate()
onStart()
onResume()
I know the answer is been accepcted, still if this helps someone I am putting it.
When app is opening for the first time, by clicking the Icon
onCreate()
onStart()
onResume()
When home button is pressed
onPause()
onStop()
when app is again opened by clicking the app icon or launched from recent
onRestart()
onStart()
onResume()
when app is opened and then back button is pressed
onPause()
onStop()
onDestroy()
The onDestroy method is called after back press. Then activity will be popped from the Activity back stack.
From docs:
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
onDestroy() from docs:
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
Activity B will be destroyed and will no longer remain in memory.
For more information please visit the official documentation for android and have a look at the activity life cycle figure.
Once you press the back key the activity's onDestroy() method will be called and the activity will be flushed out of the memory. You will then be required to restart the activity by calling the startActivity() method which will in turn call its onCreate() Method.
I would suggest to refer following link for activity lifecycle
http://stackoverflow.com/a/8516056/3110609
and following link for launch mode of activity.
www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode
After pressing the back button, Activity B will b destroyed. You see, Android Manages Activities like a Stack(an explanation of a stack). Everytime you start an activity, it pushes into the Activity Stack. So when Activity A calls Activity B, Activity B is now on top of Activity B, and when you press the back button, it also does a pop in the Activity Stack. So in concept, Activity B is gone. Pressing a Home Button is different from pressing back, it pauses the Activity, therefore it still eats a little of the phone's memory.
Here is a good explanation of how Android Manages Activities.

Open activity from the recent activities

You're on an activity and you press the home button.
Then you long press home menu button, and select the activity you were on, from the 'recent activities' screen.
What method is called when the activity shows again? onResume, onRestart or any other?
I believe onResume will be called anyways even after pause or stopped.
onRestart may be called if activity has been stopped in the background
The recommendation is to save your data in onPause and rebuild it on onResume with some flags, so flags can tell you if onResume called after onPause/onStopped or Activity is freshly created.
Taken from the Android developer website
"... When the user leaves your activity, the system calls onStop() to stop
the activity (1). If the user returns while the activity is stopped,
the system calls onRestart() (2), quickly followed by onStart() (3)
and onResume() (4). Notice that no matter what scenario causes the
activity to stop, the system always calls onPause() before calling
onStop()..."
Here is the Activity
So no Matter what onResume() would get eventually called.
You can download the ActivityDemo which exhibits the Android lifecycle accurately. This should help you.

Reconsiling the lifecycle methods and back key

In answering a question here on SO about when onDestroy is called what appears to be an inconsistency in the Android docs arose.
According to the android docs regarding the task and backstack
"When the user presses the Back button, the current activity is popped
from the top of the stack (the activity is destroyed) and the previous
activity resumes (the previous state of its UI is restored). "
However at the same time, the android lifecycle suggests that activities are not automatically destroyed but rather paused if the UI is partially hidden, stopped if the UI is totally hidden, and destroyed only if the system is low on resources.
These are two opposite positions. So my questions is - which is it?
Shout out to #Raghunandan for going back and forth with me in comments for a while. Hopefully we will get an answer.
They are both correct in their context. Maybe the lifecycle should say "destroyed only if system is low on resources OR it is popped form the stack".
Consider this for example. You start with activity A, then start activity B from A, then start activity C from B.
Now both A and B are paused and stopped, but not destroyed.
If C is heavy on resources, A or B may be destroyed.
Now press Back button from Activity C, you are back to Activity B
Activity C is paused, stopped and destroyed .
Now press Back button again, you are back to Activity A
Activity B is paused, stopped, and destroyed
System tries to keep all the activity instances so they can be re-opened quickly. But when an activity is popped from the stack, there is no way for user to re-open them in future, at least not the same instance.
There is no contradiction in these two statements.
onDestroy is called when you press BACK unless you override onBackPressed not to call finish() or do a strange thing of overriding finish and not calling super.finish().
If you don't stop the call to Activity.finish, onDestroy is always called.
The other statement speaks nothing about pressing BACK and I can find nowhere under the link you have provided here that
and destroyed only if the system is low on resources.
Additinally to onDestroy being called when app is low on resources (which strangely doesn't happen on my phone; I get OOM) it is called when 20 other Activities globally (from all applications) are started after this one.
I also encouarge you to check out yet another answer on when onDestroy is called again for a real reason of onDestroy not being called.

Multiple activities from different applications

I have two activites...one in A application and other in B application.
I am calling activity from A app to B app so A app activity will be in pause and throw an intent to B app activity
and after B app activity, I will go back again to A app activity so A app activity should start from onrestart or onresume() as it was in onPause() but it is going to oncreate(). I am sure that the A app acitivity was not destroyed because when I hit back button it starts from resume or restart state.
Basically the two app activities runs in circular fashion like
A app activity- B app Activty- A app activity - B app activity ......................... so on
One more think I see is none of them are destroyed when I hit back button then it goes to resume state so nothing is killed.
Is there any way so that my transition shld be -
A app activity - B app activity ( A pause state- B on create state)
B app activity - A app activity ( B on pause state- A resume state)
Note: two activities from in diff apps
A lot of it will depend how you go from one activity to another and how you declared them in the manifest.
Without more code, I suggest you read the documentation here, especially pay attention to taskAffinity and launchMode
I will go back again to A app activity so A app activity should start from onrestart or onresume() as it was in onPause() but it is going to oncreate()
as #Eigor mentioned - the operating system can shout activities which stopped (not visible, and somewhere in the activity stack of the current process) in this case - Android OS will shout down the Activity without going throughonDestroy() , but will go through the onSaveInstanceState() callback for giving you a chance preserving any data state you'd like to restore when the activity will re-created when you'll hit the back button. the data you saved previously will be stored in the bundle param in the onCreate()
read - http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
I guess because you don't knew that that's how the OS behaves it felt strange.
knowing that knowledge and taking it into consideration should solve your problem
I am sure that the A app acitivity was not destroyed because when I hit back button it starts from resume or restart state.
the fact that A app activity was not destroyed in this scenario is not something you can count on, and it does not contradict what I've said before. it still in some cases be shout down by the OS without onDestroy been call from the same reason.
about all the circulation you want to do between A and B: I think what I wrote can help you understand why it doesn't worked as you expect...

Android back button behaviour

Let's say we have a default, empty activity with default behaviour, launched with default intent flags. User presses back button on the device. The activity disappear... but how, actually?
Is pressing back button behaving the same way like finish()?
Is the activity immedietely destroyed (onDestroy is called)?
Is the activity guaranteed to be destroyed, but not immedietely?
Is there any chance the activity won't be destroyed and this instance will be reused when this activity is launched in the future? (so only onPause and onStop -> onStart and onResume are called?)
I'm looking for a reliable answer, so please do not answer if you are not absolutely sure what happens here.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
This is a subchapter from the official Android documentation that addresses your question. It is a subchapter of the topic Managing the Activity Lifecycle, which can be read here:
http://developer.android.com/training/basics/activity-lifecycle/index.html
It is definitely worth reading the whole chapter to get to know the details about Androids Activity behaviour. But the subchapter ( first link ) is the relevant part to this question.
you use should look into this try this
and please tell specific what you wish to do with back button for your default activities ......
When you press back, (if not intercepted by anything like the keyboard, fragment, activity, etc) the OS (via ActivityManager probably) will try to show the user the previous activity in your current task (again, ignoring fragments' back stack).
If there is no such activity, the task will be terminated and you'll go to the previous task - the home screen most of the times or some other application that might have launched your app.
You'll get onDestroy called soon (it depends on how long it takes to start the next activity but on a good phone it should be under 100-200ms).
Your activity instance won't be reused after onFinish. This happens before the activity is destroyed so if you need another activity of the same type, the OS will create another instance.
When the user presses the BACK key, the current activity is popped from the top of the stack (the activity is guaranteed to be destroyed, but not immediately, may be when the system resources are low) and the previous activity resumes (the previous state of its UI is restored).
Which actions does the back button/back key on Android trigger?
Definitly onDestroy() is called .....There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish().

Categories

Resources