What methods are invoked in the Activity Lifecycle in the following cases: - android

Let's say I have a Hello World single Activity application. I start this application.
What methods are invoked in each case:
Home button is pressed: ?
Back button is pressed: ?
Phone call is received: ?
What methods are invoked once the user starts the application again via the app icon (assuming the OS hasn't had a "other apps need memory condition"):
Home button was pressed: ?
Back button was pressed: ?
Phone call was received: ?
Thanks all.
Edit: Extra Credit: How can the user invoke onPause without invoking onStop?

both pressing home button and receiving a call don't remove the activity from the task's stack, and will be available when you re-enter the app => onPause() => onStop().
as the activity lifecycle diagram shows, re-entering the app calls => onRestart() => onStart() => onResume()
pressing the back button instead kills the activity => onPause() => onStop() => onDestroy()
re-entering the app in this case calls the classics => onCreate() => onStart() => onResume()
EDIT
from http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
If an activity has lost focus but is
still visible (that is, a new
non-full-sized or transparent activity
has focus on top of your activity), it
is paused. A paused activity is
completely alive (it maintains all
state and member information and
remains attached to the window
manager), but can be killed by the
system in extreme low memory
situations.

There can be several scenarios
Opening the app from the app icon. following methods are called
onCreate()-->onStart()-->onResume()
When user presses the home button
onPause()-->onStop()
When user returns to the app from the Activity Stack
onRestart()-->onStart()--> onResume()
When the app is running and user presses the power button
onPause()-->onStop()
When user unlocks the phone
onRestart()-->onStart()--> onResume()
When user gets an incoming call while you are in the app
onPause()
When user returns to the app after disconnecting the phone call
onResume()
When user presses the back button from the app
onPause()-->onStop()-->onDestroy()
And when the user presses the home button and from the activity stack user swipes the app.onDestroy() method may or may not be called depending upon the OS contains the context of the Activity or not according to memory requirements

Well see, while a sequence of events may occur with your hello world program, the same sequence may not occur in say a video game, because Android will probably Destroy it for taking up too much resources.
The best way I have found to see the lifecycle for my app is to override all the methods (onStart, onRestart,..., including the onSaveInstance and onRestoreInstance) and insert log statements in each one. Like so:
#Override
public void onDestroy() {
// Call the super class
super.onDestroy();
// Log the action
Log.d("Debug", "onDestroy() has been called!");
}
Now I can go to logcat and see what events took place.

Related

Does suspended state exist in Android?

In iOS if user press home button, app will move to suspended state. Double click of the home button, suspended apps appear, and selecting an app will bring back it to active state.
How is it by Android? App can become to suspended state pressing center button? Or app move to not running state?
If suspended state exist, how to bring back to running state, where I can select one app among many?
In an activity the oncreate method will get called only once, when app first starts? Which method is get called that will called always when app appears, come back to running state?
Have a look over Android Activity Lifecycle
In android, if the app is not "on the main screen" it counts as Paused state.
By starting an app, there will be a one and only call for onCreate, later will be called onStart and onResume. If you will now click the home button, the onPause and onStop method will be called. Once you wish to back to the activity you can press the Overview button, and select your app from the list and the method onRestart, onStart and onResume will then be called.
If your app is not fully hidden, e.g you swing your finger from top to bottom to see "notification center", this will cause skipping the onStart, onStop and onRestart functions.

The activity loses focus when home key button is pressed on Android

When I press the home button on my phone, and when I return to the application, it loses the focus completely, it loses track of where activity was; i.e. the Activity returns to principal state. How to avoid that so that when I press the home key and return to the app; to return to the where the activity was?
I need the solution to work for all devices?
Thanks.
Well when you press the home button, the activity's lifecycle methods will be called (first onPause(), then onStop()), and then when you return, if in the meantime your operating system didn't call onDestroy() (this happens when you force close an app or are running out of memory), your Activity will call onResume() method. If it was destroyed, it will call onCreate and start from the beginning.
So basically what you need to do is override (CTRL+O in Android studio to open the override menu) these methods to save your application state, save the important parameters of your app (since you've not shared any code, I cannot presume what these are) and to then later restore the state your app had previously come to.

Android Activity life cycle - onPause() and onResume()

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

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.

Difference between android Home key and Back key and their behaviour

Can anyone point me out or explain what is the difference between android Home key and Back key and their respective behavior related to an android app/activity.
Thank you.
Back Key :
If you press Back Key, onPause(), onStop() and onDestroy() callbacks will called.
Activity will created again by system calls onCreate() callback, then onStart() and onResume() callbacks will be followed.
Home Key :
If you press Home Key, onPause() and onStop() callbacks will called.
Here Activity will restart by system calls onRestart() callback, then onStart() and onResume() callbacks will be followed.
In addition to #Fosco's comments, using back will usually cause an app to exit, where home will leave it running. This is dependent on the application, but the general pattern is to exit the app when using back on the last activity.
Back key destroys the current Activity, home key doesn't. In the Activity lyfecycle, pressing back calls all the way to current activity's onDestroy() method. On the other hand, pressing home pauses the Activity, which stays alive in background.
The home key takes you to the home screen, the back key takes you back to the previous activity (or home if there's no activity to go back to.)
If you are at the home screen and launch Messaging, then hit back, it's the same as hitting the home key.
If you're in Email and get an alert for a text message, and you choose the notification which takes you to Messaging, then hit Back, you'll go back to Email.
edit: as mentioned by Tim Coker, when the back button takes you to the home screen, it usually terminates the activity. I think this is based on the app, whether it terminates or stays resident.

Categories

Resources