I have an App, which uses an ActicityGroup to have a stack of Activitys. If I start a new Activity, I do this as Child of my ActivityGroup. Lets assume I'm in my starting Activity (1), and I start a new one(2), so here is what's getting called:
(1): onPause()
(2): onCreate(), onStart(), onResume()
until here, everything as aspected. if I press my BackButton the stack is as following:
(2): onPause(), onStop(), onDestroy()
(1): onStop(), onDestroy() [sic]
(1): onCreate(), onStart(), onResume()
I see no reason,first why (1) should perform onStop, and onDestroy(), to reCreate again, and second why onRestart never gets called on (1).
Has anyone a Reason for this behavior? Can I 'cancel' calls to onStop() or onDestroy() somehow?
any idea apreciated
Try using FLAG_ACTIVITY_SINGLE_TOP when starting child activity, like:
Window window = getLocalActivityManager().startActivity(id,
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
More info here:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP
You can avoid Activity onDestroy method when back button pressed in a very simple way,
Just mention
android:persistent="true"
for your activity, that should prevent your activity getting destroyed, To know more in detail you can visit the below SO post
Prevent activity from being destroyed as long as possible
when you press back button it's default behavior is stop or destroy the current activity you can override the back button. If you press home button then it will use the onPause() that means os will consider that you want to continue with current activity when you again launch same activity but if you press back button it means that you finish your current activity and back to the last activity
Onstop() and onDestroy() will not call untill you did not finish activity. Ondestory() called for releasing of resource that occupied by Activity. IF activity use has been over then it is better to destroy this activity.
Second it will be good for memory management scheme. and if you do not call destroy then it will automatically call when you exit from app
and finally
if you doesnot want to call destroy when you press back button then you can use override method onBackpressed()
Related
I know that In Android, you won't get a guaranteed activity's onDestroy() call. My question is in which scenarios onDestroy() is not called?
Suppose there are two activities A and B.Activity A contains a next button and B contains a back button.If you click next,then Activity B will be launched.At that point of time onDestroy() is not called.onStop() of activity A is called after onResume() of B.
Then when you click the back button in Activity B,then onRestart() of A will called after onStop() of B.In this case also onDestroy() will be not called.
When you press the back button of the device onDestroy() will be called at that point of time or if you call finish() while navigating from A to B.
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.
In the Android developer diagram, I saw that onResume() is always called before onPause(). Assuming the user starts a new Activity, why should onPause() be preceded by onResume()?
I mean:
OnResume can be called in 2 occassions:
1) when user starting new activity (before OnPause)
2) when activity is in background and if the user brings the activity back to the
foreground
I expect in every case, something else should be done.
You are getting it wrong. Whenever an activity is created, onResume is called just after onStart. Whenever your activity goes back stack onPause is called. Again if your activity comes back to foreground then onResume is called. So, it is not like, onResume is called before onPause. Whenever activity is returning from onPause state, onResume gets called instead of onStart or onCreate. This happens so that Android does not have to create Activity instance again and again though those instances are not properly destroyed. This is quite memory efficient too.
NOTE: If you app is running and the user presses back button or home button, then the Activity goes through onPause() and onStop() state. After this if the user is again coming back to your app then, onRestart(), onStart() and onResume() will be called sequentially.
Then when the activity is only in onPause() state ? When a dialog surfaces on top of your activity or your activity is getting displayed in Split screen but it doesn't have focus (user is not interacting with your app). On these cases, activity goes to onPause() state only.
onResume() is always called before onPause()
This is correct. onResume is always called when the Activity is launched for the first time, before onCreate, and when the Activity is being resumed (user navigates back to your Activity)
Assuming the user starts a new Activity, why should onPause() be
preceded by onResume()
onPause is only called when the Activity is put to background, or before onDestroy if the Application is being destroyed. So onPause is always being called after a call to onResume has been made. Why? Because that's the lifecycle of the Activity as defined by the Android framework.
The life cycle of the activity is as follows
Fresh start via onCreate(), onStart(), onResume .... and close via onPause()->onStop()->onDestroy()
Yellow background: Activity goes into background and thus is no longer visible. The user returns back to the activity.
e.g.
Switch off the phone screen while the activity is running: onPause()->onStop()
Switch on the screen again: onStart() -> onResume()
Green background: The activity stays in the visible screen area but is not active
e.g. Activate multiple windows (split screen) occupying one part of the screen each and tip on your app to make it active
tip on the other app: onPause() is called in your app as it goes into pause but is still visible
tip on your app: onResume() is called
Here is an example of a split screen with two apps:
see android documentation on activity life cycle for details
I am new to android.I have learned that the onPause() method is called when some other activity comes in the foreground and onStop() method is called when an activity is no longer visible.Can someone please explain what's the difference between going in background and becoming invisible giving some practical example.
Best example? A popup, your activity is still visible, so only onPause() is called. Becoming invisible is when another activity is launched, the app is sent to the background, the user switches to another app et.
Think about two cases
1. Calling Activity B as dialog
In this case, some part of Activty A will be visible. So only onPause() method will get called for Activity A. And when you will be back from Activity B to A, onResume() of Activity A will get called.
2.Calling Activity B as normal activity
In this case, Activity A will be completely invisible, So onPause() and onStop() both will be called. And when you will be back from Activity B to A, onRestart(), onStart() and onResume() methods of Activity A will be called.
Have a look
Case 1: If you call normal activity. (Which is going to hide caller activity)
onPause()
onStop()
<------- Back to caller
onRestart()
onStart()
onResume()
Case 2: If you call activity-as-dialog. (Which is going to partially hide caller activity)
onPause()
<------- Back to caller
onResume()
whenever a dialog box appears on any activity then activity's onPause() called and this activity holds the first position in stack(Top), on the other hand whenever we switches activity from one to another the previous activity's on Stop() method called and its automatically garbage collected in sometimes.
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.
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.