Difference between onStart() and onResume()
Why can't it be the onResume() is invoked after onRestart() and onCreate() methods just excluding onStart()? What is its purpose?
OK, as my first answer was pretty long I won't extend it further so let's try this...
public DriveToWorkActivity extends Activity
implements onReachedGroceryStoreListener {
}
public GroceryStoreActivity extends Activity {}
PLEASE NOTE: I've deliberately left out the calls to things like super.onCreate(...)
etc. This is pseudo-code so give me some artistic licence here. ;)
The methods for DriveToWorkActivity
follow...
protected void onCreate(...) {
openGarageDoor();
unlockCarAndGetIn();
closeCarDoorAndPutOnSeatBelt();
putKeyInIgnition();
}
protected void onStart() {
startEngine();
changeRadioStation();
switchOnLightsIfNeeded();
switchOnWipersIfNeeded();
}
protected void onResume() {
applyFootbrake();
releaseHandbrake();
putCarInGear();
drive();
}
protected void onPause() {
putCarInNeutral();
applyHandbrake();
}
protected void onStop() {
switchEveryThingOff();
turnOffEngine();
removeSeatBeltAndGetOutOfCar();
lockCar();
}
protected void onDestroy() {
enterOfficeBuilding();
}
protected void onReachedGroceryStore(...) {
Intent i = new Intent(ACTION_GET_GROCERIES, ..., this, GroceryStoreActivity.class);
}
protected void onRestart() {
unlockCarAndGetIn();
closeDoorAndPutOnSeatBelt();
putKeyInIgnition();
}
OK, so it's another long one (sorry folks). But here's my explanation...
onResume()
is when I start driving and onPause()
is when I come to a temporary stop. So I drive then reach a red light so I pause...the light goes green and I resume. Another red light and I pause, then green so I resume. The onPause() -> onResume() -> onPause() -> onResume()
loop is a tight one and occurs many times through my journey.
The loop from being stopped back through a restart (preparing to carry on my journey) to starting again is perhaps less common. In one case, I spot the Grocery Store and the GroceryStoreActivity
is started (forcing my DriveToWorkActivity
to the point of onStop()
). When I return from the store, I go through onRestart()
and onStart()
then resume my journey.
I could put the code that's in onStart()
into both onCreate()
and onRestart()
and not bother to override onStart()
at all but the more that needs to be done between onCreate() -> onResume()
and onRestart() -> onResume()
, the more I'm duplicating things.
So, to requote once more...
Why can't it be the onResume() is invoked after onRestart() and onCreate() methods just excluding onStart()?
If you don't override onStart()
then this is effectively what happens. Although the onStart()
method of Activity
will be called implicitly, the effect in your code is effectively onCreate() -> onResume()
or onRestart() -> onResume()
.
Android app: onResume() and onStart()
Please Refer to the Android Activity Lifecycle Documentation.
onStart
is called when your application first starts.
If the user click the home button, or another app takes focus, onPause
will be called.
If the activity regains focus, while stil running on the device, onResume
will be called, and onCreate
will NOT be called again.
If the user uses the activity manager to close the application, and then relaunches it, onCreate
will be called again.
Note, every time onCreate
is called, onResume
is also called.
When does onResume() run without onStart()?
There's documentation on it.
The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.
The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.
As I understand it, onStart()
and onStop()
represent visibility, while onResume()
and onPause()
represent priority.
For example, if you open your app, both onStart()
and onResume()
will be called. With your app still open, say you then get a Facebook Message and open the chat. onPause()
will be called, but onStop()
won't. Your app is no longer in the foreground, but it's still visible.
EDIT:
I know I linked the Activity documentation, but according to the Fragment documentation:
onStart() makes the fragment visible to the user (based on its containing activity being started).
onResume() makes the fragment begin interacting with the user (based on its containing activity being resumed).
onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
The same principle applies. In most cases, it's just a direct call from the Activity.
Where is the difference between onCreate and onStart if both are called upon Activity change anyway? What's the purpose?
onCreate()
is called when the activity is first created. onStart()
is called whenever the activity becomes visible, which includes when it is first created (after onCreate()
) and after it is coming back to the screen from being stopped (e.g., another activity took over the screen).
So:
Put code in
onCreate()
that needs to happen when the activity is created (and useonDestroy()
to clean it up)Put code in
onStart()
that needs to happen either when the activity is created or when the activity returns to the foreground (and useonStop()
to clean it up)
Frequently, we do not do anything special when the activity returns to the foreground, in which case you do not need to worry about onStart()
or onStop()
.
When will onResume() be called without onStart() being called first (Fragment Lifecycle)
When your activity stays visible while showing a dialog or another activity is shown on top that has transparency.
Basically your activity is visible between onStart()
and onStop()
and your activity is interactive between onResume()
and onPause()
. When it it becomes uninteractive while staying visible you will get onPause without onStop.
Difference between onCreate() and onStart()?
Take a look on life cycle of Activity
Where
***onCreate()***
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.
Always followed by onStart().
***onStart()***
Called when the activity is becoming visible to the user.
Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.
And you can write your simple class to take a look when these methods call
public class TestActivity extends Activity {
/** Called when the activity is first created. */
private final static String TAG = "TestActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, "On Create .....");
}
/* (non-Javadoc)
* @see android.app.Activity#onDestroy()
*/
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "On Destroy .....");
}
/* (non-Javadoc)
* @see android.app.Activity#onPause()
*/
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "On Pause .....");
}
/* (non-Javadoc)
* @see android.app.Activity#onRestart()
*/
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "On Restart .....");
}
/* (non-Javadoc)
* @see android.app.Activity#onResume()
*/
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "On Resume .....");
}
/* (non-Javadoc)
* @see android.app.Activity#onStart()
*/
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "On Start .....");
}
/* (non-Javadoc)
* @see android.app.Activity#onStop()
*/
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "On Stop .....");
}
}
Hope this will clear your confusion.
And take a look here for details.
Lifecycle Methods in Details is a very good example and demo application, which is a very good article to understand the life cycle.
Does anybody knows the difference between onResume() and onRestart()?
When OnResume gets called ?
User is in Activity 1 and now traverses to Activity 2. (Activity 1 is not visible at all). Now User clicks back button from Activity 1, OnResume method of Activity 1 gets called.
When OnRestart gets called ?
When user traverses from Activity 1 to Activity 2 as in above case, moves back to Activity 1, onRestart gets called.
so what is the difference ?
OnResume method gets called everytime when an activity moves between background to foreground state.
But, onRestart gets called only when onStop method is called.
Please note that, the system also calls the onStart() method, which happens every time your activity becomes visible (whether being restarted or created for the first time). The onRestart() method, however, is called only when the activity resumes from the stopped state.
When to use OnRestart Method ?
Use this when you want to perform special restoration work that might be necessary only (if you have handled onStop method for releasing ay CPU intensive resources) if the activity was previously stopped, but not destroyed.
Related Topics
Scale Image to Fill Imageview Width and Keep Aspect Ratio
Full Webpage and Disabled Zoom Viewport Meta Tag for All Mobile Browsers
Admob Error in Eclipse for Android:Configchanges
Difference Between Activity and Fragmentactivity
How to Use Assert on Android Devices
Trusting All Certificates with Okhttp
Passing Data Between Activities in Android
How Does the Mapping Between Android Resources and Resources Id Work
Android - Get Children Inside a View
How to Pause/Resume Thread in Android
Detect an Application Is Installed or Not
Using Contextmenu with Listview in Android
How Do Android Screen Coordinates Work
How to Add a Library (Android-Support-V7-Appcompat) in Intellij Idea