Raja Jawahar
Raja Jawahar

Reputation: 6952

When do onRestart method get called in Android?

While we are having onStart method, what is the purpose of onRestart method?

@Override
    protected void onStart() {
        super.onStart();
    }



 @Override
    protected void onRestart() {
        super.onRestart();
    }

Upvotes: 20

Views: 31178

Answers (8)

Rissmon Suresh
Rissmon Suresh

Reputation: 14053

Adding to NightFury's answer. The onRestart() method is a callback method in the Android Activity lifecycle that is called when the activity is restarting after having been stopped.

Some use cases:

  1. If an activity is paused or stopped and the user returns to it, the onRestart() method can be used to resume the activity from its paused or stopped state.
  2. In some cases, the onRestart() method can be used to refresh the data displayed in the activity. For example, if the activity displays a list of items that are fetched from a server, the onRestart() method can be used to refresh the list after the user returns to the activity.
  3. When the configuration of the device changes (such as screen rotation), the onRestart() method is called. This can be used to update the activity's layout or other resources to accommodate the new configuration.
  4. Error handling: If an activity has encountered an error and was stopped, the onRestart() method can be used to reset the activity and handle the error.

Upvotes: 0

NightFury
NightFury

Reputation: 13546

One case of onRestart() being called is when user presses home button and comes to launcher screen. In this case, activity is not destroyed and pause/stop events are fired. When user opens your app again, onRestart() for that activity is called before onStart(). You can find example here.

Upvotes: 11

Brian Vo
Brian Vo

Reputation: 1001

Only 100% sure that onRestart is called is when you navigate away and then navigate back to the activity.

On the other hand, when you press home button and the app moved to background, there is no way we can know whether the app is destroyed by OS to claim back resources or it is still reside on memory. If the app is destroyed then onCreate will be called. Otherwise if the app is still available in memory then onRestart will be called.

Upvotes: 2

EMEM
EMEM

Reputation: 3138

onRestart() is called after onStop() when the current activity is being re-displayed to the user.

e.g. The user has navigated back to it.

Upvotes: 0

Shalu T D
Shalu T D

Reputation: 4039

The onRestart() method will be called whenever the Activity comes back from the invisible state. Suppose, we pressed the home button of the device and coming back, this onRestart() will be invoked. For more info about this, please go through the documentation

Upvotes: 9

Karol Żygłowicz
Karol Żygłowicz

Reputation: 2452

Here is the activity lifecycle there is your onStart() and onRestart() methods with explanations

enter image description here

more info here

Upvotes: 23

BRG
BRG

Reputation: 380

According to this

Note: Because the system retains your Activity instance in system memory when it is stopped, it's possible that you don't need to implement the onStop() and onRestart() (or even onStart() methods at all. For most activities that are relatively simple, the activity will stop and restart just fine and you might only need to use onPause() to pause ongoing actions and disconnect from system resources.

Called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it). It will be followed by onStart() and then onResume().

For activities that are using raw Cursor objects (instead of creating them through managedQuery(android.net.Uri, String[], String, String[], String), this is usually the place where the cursor should be requeried (because you had deactivated it in onStop().

Upvotes: 1

Nikola
Nikola

Reputation: 2132

You can read all about the Activity's lifecycle on Android developers: http://developer.android.com/reference/android/app/Activity.html#onRestart()

Taken directly from there:

Called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it). It will be followed by onStart() and then onResume().

For activities that are using raw Cursor objects (instead of creating them through managedQuery(android.net.Uri, String[], String, String[], String), this is usually the place where the cursor should be requeried (because you had deactivated it in onStop().

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

Upvotes: 3

Related Questions