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

running-order

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:

class MainActivity : AppCompatActivity() {

    private val TAG: String = "Running Order"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Log.d(TAG, "onCreate in called")
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart in called")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume in called")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause in called")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop in called")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy in called")
    }
}

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

Life cycle of fragment

class MyFragment : Fragment() {

    private val TAG: String = "Running 2Order"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        Log.d(TAG, "running onCreateView")
        return inflater.inflate(R.layout.fragment_my, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.d(TAG, "running onViewCreated")
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        Log.d(TAG, "running onActivityCreated")
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "running onStart")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "running onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "running onStop")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.d(TAG, "running onDestroyView")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "running onDestroy")
    }



    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment MyFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            MyFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }
}

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.

onDestory(): 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

Reference

comments powered by Disqus