Android: Are you familiar with onCreate(), onStart() onResume() and …

Needone App
2 min readJul 6, 2020

--

Before this, let me ask you what is the running order of onCreate(), onStart(), onResume(), onDestory()? And when can I override the onCreateView()?

Many people get confused when and what method should they call when they start the Android development. This article is the quick guide give you an overview on this.

Two main Views in the Android, Activity and Fragment.

Activity

Here is a demo on the running order:

The output of first start up:

onCreate in called 
onStart in called
onResume in called

The output of go back to mobile home screen:

onPause in called 
onStop in called
onDestroy in called

The output of going back to app from background:

onCreate in called 
onStart in called
onResume in called

Fragment

The output of first start up:

running onCreateView 
running onViewCreated
running onActivityCreated
running onStart

The output of go back to mobile home screen:

running onPause 
running onStop
running onDestroyView
running onDestroy

The output of going back to app from background:

running onCreateView 
running onViewCreated
running onActivityCreated
running onStart

Update on Activity lifecycle methods:

onCreate(): This method will get called when the activity is created. Here we can have some initialisation work done here, such as load the layout xml file.

onRestart(): The current Activity is being restarted, from invisible to visible.

onStart(): Activity is created but not visible yet.

onPause(): Activity is being stopping but still visible. Normally the onStop() will be called next to it. onPause() can't have too much heavy duty work, as new Activity's onResume() starts only when onPause() finished.

onResume(): Activity is visible, so users can interact with it.

onStop(): Activity is about to be destroyed. You can do some clean up work here, but not too heavy.

onDestroy(): Activity is about to be destroyed. This is the last method in the life cycle get called.

Update on Activity lifecycle when screen rotates:

When the screen is rotated, activity will be destroyed and recreated:

After google around, someone online gives the answer on the running order:

onPause()->onSaveInstanceState()-> onStop()->onDestroy()->onCreate()->onStart()->onRestoreInstanceState->onResume()

However, from the testing code I can only find out the following

onPause
onStop
onDestroy
onStart
onRestoreInstanceState
onResume

If you like it, please 👏👏👏, I will continue to share more tutorials.

Reference

https://needone.app/android-are-you-familiar-with-oncreate-onstart-onresume/

Originally published at https://needone.app on July 6, 2020.

--

--

No responses yet