Android Activity Lifecycle and Fragment Lifecycle, States and Method Descriptions

Süleyman Başaranoğlu
4 min readAug 8, 2021

--

Events such as stopping the application, pressing the back button, backgrounding the application, changing the rotation of the application all build up the Android lifecycle. In this article, I will talk about what kind of events occur while these functions are taking place. Before starting the topic, the picture (1.1) that includes the Activity Lifecycle and the Fragment Lifecycle together provides us with important information about the subject content.

Activity Lifecycle — Fragment Lifecycle 1.1

It is necessary to examine the events mentioned above in order to better understand this life cycle.

#1#When we first start the Activity by opening our application (Picture 1.2)#

1-onCreate() (runs when Activity first starts)
2-onStart()( works after onCreate or onRestart)
3-onResume()(called after onRestoreInstanceState(Pack),onRestart() or onPause())

#1#When we first launch the Fragment by opening our application (Picture 1.2)#

1-onAttach() (Allows adding Fragment Activity and it is created first)
2-onCreate() (Stores data)
3-onCreateView()(XML connections)
4-onActivityCreated()(When you wait for Activity to be created, UI elements inside it are accessed)
5-onStart()
6-onResume()

Activity Lifecycle — Fragment Lifecycle First Launch 1.2

#2#Activity when the back button is pressed (Picture 1.3)#

#1# as a continuation
1-onPause() (onRestoreInstanceState(Pack), called after onRestart())
2-onStop() (Called when you are no longer visible to the user)
3-onDestroy() (Activity has completed its lifecycle)

#2#Fragment when back button is pressed (Picture 1.3)#

#1# as a continuation
1-onPause()
2-onStop()

3-onDestroyView() (Fragment XML is removed)
4-onDestroy()
5-onDetach()(Leaves Fragment Activity)

Activity Lifecycle — Fragment Lifecycle Back Pressed 1.3

#3#Activity when the phone is rotate (Picture 1.4)#

1-onPause()
2-onStop()
3-onSaveInstanceState()( Called before an Activity is killed so it restores it to the state when it returns in the future)
4-onDestroy()
5-onCreate(Bundle)
6-onStart()

7-onRestoreInstanceState()(Performs restoring any view state previously frozen by onSaveInstanceState(Bundle))
8-onResume()

#3#Fragment when the phone is rotate (Picture 1.4)#

1-onPause()
2-onStop()

3-onSaveInstanceState()(Called before a Fragment is killed so it restores it to the state when it returns in the future)
4-onDestroyView()(XML is removed)
5-onDestroy()
6-onDetach()
(Leaves Fragment Activity)
7-onAttach()
8-onCreate(Bundle)

9-onCreateView(Bundle)(XML connections)
10-onActivityCreated(Bundle)
11-onStart()
12-onResume()

Activity Lifecycle — Fragment Lifecycle Rotate 1.4

Let’s look at different situations now.

#4#When you press the home button or switch to another Activity (Picture 1.5)#

1-onPause()
2-onStop()

3-onSaveInstanceState()
When we go back to Activity
4-onRestart()
5-onStart()
6-onResume()

#4#When you press the home button or switch to another Fragment (Picture 1.5)#

1-onPause()
2-onStop()

When we come back to Fragment
3-onStart()
4-onResume()

Activity Lifecycle — Fragment Lifecycle Click Home Button 1.5

#5#When the application pop-up or Dialog is opened or when the phone rings in Activity#

1-onPause()
When closed
2-onResume()

#5# When Pop-Up comes in application or Dialog is opened or Phone rings in Fragment#

1-onPause()
When closed
2-onResume()

#6#Activity# when the phone screen is turned off and then turned on#

1-onPause()
2-onStop()

When opened
3-onRestart()
4-onStart()
5-onResume()

#6#Fragment# when the phone screen is turned off and then on#

1-onPause()
2-onStop()

When opened
3-onStart()
5-onResume()

Other than these, I would like to mention some of the methods used below.

onContentChanged() : Called when the content view of the screen changes.

onPostCreate(): Called after onStart() is called when the activity initialization is complete.

onRestoreInstanceState(): This method is called between onStart() and onPostCreate(Bundle). Used when restarting a previously saved state in savedInstanceState.

onPostCreate(Bundle): Used when restarting a previously saved state in savedInstanceState.

onPostResume(): Called after onResume() when the activity is resumed.

onAttachedToWindow(): Called when a new window is added and associated with setContentView.

onCreateOptionsMenu(): Called when the options menu is displayed for the first time.

onPrepareOptionsMenu(): Called again each time the options menu is displayed.

onUserInteraction(): Called when the user touches the screen.

onUserLeaveHint(): Called when the user presses the Home button, but interrupted events are not called. (Incoming phone call etc.)

onActivityResult(): Called when the user exits the Activity and back to Activity.

onAttachFragment(): Called after onAttach() is called and when a Fragment is previously attached.

onConfigurationChanged(): It is used to notify the Activity of the settings changed by the system in the configChanges property while defining an Activity on the Manifest file.

Resources :

https://developer.android.com/guide/fragments/lifecycle

--

--

No responses yet