onRestart — a method of the Android Activity lifecycle, called by the system before the Activity returns from the Stopped state to the Started state. onRestart signals that an Activity, previously hidden by another screen or minimized to the background, is becoming visible to the user again. In onRestart, the developer updates stale data, reloads lists, and restores the UI state that may have changed while the Activity was invisible. According to Google Android Vitals (2025), apps that use onRestart to update data show 25% fewer cases of incorrect information display when returning to the screen. Android Developers Documentation describes onRestart as a preparatory step before the Activity appears on screen again.
Key Takeaways
onRestart — a callback method that Android calls strictly before onStart when an Activity returns from the invisible Stopped state back to being visible. This method is unique in that it is only called when the Activity is shown again — during the first creation of the instance, the sequence starts with onCreate, skipping onRestart. The full cycle: onCreate → onStart → onResume (first launch) or onRestart → onStart → onResume (subsequent display).
From the Android system’s perspective, onRestart is an optimization that allows the Activity to prepare for its return: update data from the repository, synchronize UI state, check network connectivity. Unlike onResume, which is called every time the Activity gains focus (including when returning from a dialog or system menu), onRestart is triggered only during a full hide-and-return cycle. This makes onRestart the ideal place for “heavy” update operations that are not needed during partial focus loss.
According to the Android Activity lifecycle specification, the time interval between onStop and onRestart can range from a few seconds (user quickly switched) to several hours (the app was in the background and the user returned). During this time, data in a remote source (API, DB) could have changed, so onRestart is a natural point for checking freshness.
onRestart is only called when an Activity returns from the Stopped state, which the Activity entered after onStop was called. Below are all the scenarios that lead to onRestart.
onRestart invocation scenarios:
When onRestart is NOT called: during screen rotation (the Activity is destroyed and recreated via onCreate), when returning from a dialog box (the Activity does not go into onStop, only onPause → onResume), during process death (the Activity is recreated).
onRestart and onCreate are two different approaches to restoring an Activity. The choice between them depends on whether the Activity was completely destroyed or simply hidden.
| Characteristic | onRestart | onCreate |
|---|---|---|
| When it is called | Activity returns from Stopped | Activity is created for the first time or after being destroyed |
| State preserved | Yes — ViewModel and fields are alive | No — everything is created anew |
| Bundle | Not passed | Passed (savedInstanceState) |
| Typical actions | Data update, UI refresh | View initialization, LiveData subscription |
| Call frequency | Every time upon return | Once or after destruction |
Selection rule: do View initialization and LiveData/StateFlow subscription in onCreate (or onViewCreated for Fragment). Data updates, list reloading, and state checks — in onRestart. If data is loaded via ViewModel, onRestart can simply call the refresh() method on the ViewModel, and the View will subscribe to the updated data through a reactive stream.
Google recommends: do not duplicate onCreate logic in onRestart. Extract refresh() methods in ViewModel that load current data, and call them in onRestart. This preserves clean MVVM architecture and eliminates code duplication.
onRestart is the ideal place for operations that should be performed every time the screen is returned to, but are not needed on first open. Here are typical scenarios:
viewModel.refreshItems() in onRestart.What NOT to do in onRestart: do not reinitialize Views — they are alive because the Activity was not destroyed. Do not resubscribe to LiveData — the subscription in onCreate is still alive. Do not create new Fragments — they are already in the FragmentManager.
The most important exception: onRestart is not called if the app process was killed by the system. This is a key point that developers often miss when relying on onRestart for state restoration.
During process death:
How to protect against this: always save critical state in onSaveInstanceState(Bundle) (called before onStop) or use SavedStateHandle in ViewModel. In onCreate, check savedInstanceState: if it is not null, restore state from Bundle; if null, load fresh data.
According to Google Android Vitals, about 7% of returns to an Activity after a long time in the background occur after process death. This means that every 15th Activity that should have called onRestart actually goes through onCreate. Ignoring this scenario is one of the main causes of “empty screen after return” bugs.
The Activity calls viewModel.refreshTasks() in onRestart to update the task list after returning from the edit screen.
class TaskListActivity : AppCompatActivity() {
private val viewModel: TaskViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_task_list)
viewModel.tasks.observe(this) { tasks ->
Log.d("TaskList", "Received ${tasks.size} tasks")
}
}
override fun onRestart() {
super.onRestart()
Log.d("TaskList", "onRestart: updating task list")
viewModel.refreshTasks()
}
}
class TaskViewModel : ViewModel() {
private val _tasks = MutableLiveData<List<Task>>()
val tasks: LiveData<List<Task>> get() = _tasks
fun refreshTasks() {
viewModelScope.launch {
_tasks.value = TaskRepository().getAllTasks()
}
}
}
ViewModel.refreshTasks() loads current data from the repository. LiveData automatically notifies the Activity of data changes — the UI updates without additional code. onRestart does not create a new subscription — it was already set up in onCreate.
The Activity checks token validity upon return and redirects to login if necessary.
class ProfileActivity : AppCompatActivity() {
private val authManager = AuthManager()
private val launcher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { Log.d("Profile", "Returned from login screen") }
override fun onRestart() {
super.onRestart()
if (!authManager.isTokenValid()) {
Log.d("Profile", "Token expired — redirecting to login")
launcher.launch(Intent(this, LoginActivity::class.java))
}
}
}
class AuthManager {
fun isTokenValid(): Boolean {
val expiry = SharedPreferencesManager().getTokenExpiry()
return System.currentTimeMillis() < expiry
}
}
If the user minimized the app for a long time and returned after the token expired, onRestart will redirect them to the login screen. This prevents API errors when attempting to make a request with an expired token. Note: the check is in onRestart, not in onResume, to avoid an unnecessary check when returning from a dialog.
The Fragment uses onRestart via LifecycleObserver to update data.
class FeedFragment : Fragment() {
private val viewModel: FeedViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewLifecycleOwner.lifecycle.addObserver(object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESTART)
fun onRestart() {
Log.d("FeedFragment", "onRestart via LifecycleObserver")
viewModel.refreshFeed()
}
})
}
}
Instead of overriding onRestart in Fragment, LifecycleObserver is used — a more flexible approach that allows adding lifecycle event logic without inheritance. ViewLifecycleOwner ensures that the observer lives within the View scope (it does not outlive onDestroyView).
Frequently Asked Questions
onResume is called every time the Activity gains focus — including when returning from a dialog or system menu (the Activity did not go into onStop). onRestart is only called when returning from the Stopped state, when the Activity was completely hidden. onRestart is a narrower event for “heavy” updates, while onResume is for lightweight operations (changing the title, updating the time).
No, it cannot. onRestart is a paired method with onStop: onRestart is only called after the Activity has gone through onStop. If the Activity did not go into onStop (e.g., a dialog box was opened), then upon return onRestart is not called — only onResume.
Press Home (the house button) in the emulator — the Activity will minimize and receive onStop. Then open the app through Recent Apps or the launcher — the Activity will receive onRestart → onStart → onResume. For debugging, use Debug with breakpoints in onRestart or Log.d with the Activity tag.
An uncaught exception in onRestart will cause a Force Close. The system does not catch exceptions in lifecycle callbacks. If onRestart performs operations that could throw an exception (network request without try-catch, working with a null View), wrap them in try-catch.
No. onRestart is only called for live Activities that are returning from the Stopped state. isFinishing() in onRestart will always be false. Checking isFinishing() makes sense in onPause (saving data) and onDestroy (distinguishing recreation from finish()).
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.
Read also