Android Activity Life Cycle

OnCreate ()

Activity is the first invoked method.
If the finish () method is invoked, the onDestroy () method is entered directly. Other methods in the Activity life cycle are not called.
@param savedInstanceState contains the values ​​provided from the onSaveInstanceState (Bundle) or onPause method if Activity needs to be recreated in some way. Otherwise it returns null.

You will see two log processes above. The first is before the setContentView () method is called, and the other is after. The reason I show this is because the call to the onContentChanged () method will be called after the call to the setContentView () method. So if we had just logged after setContentView (), we would think that the method that was running first was onContentChanged (). We do two logging to see this difference.

I wanted to explain this question, which also frequently asked for at stackoverflow.

onCreateView ()

Called when filling your Activity with LayoutInflater. OnCreate has a similar operating logic.
You can use the UI elements in the XML file to link to your Activity class.
If you are accessing UI elements with onCreate (Bundle), it is pointless to do this with onCreateView (). Therefore, it has been commented.

onContentChanged ()

Called if the view of the Application UI changes in any way
Example: notifyDataSetChanged, setContentView, addContentView causes this change.

onStart ()

Called after onCreate (Bundle). Or, if Activity is in some way onStop (), it is called after onRestart ().

onRestoreInstanceState ()

If the activity needs to be recreated in some way, as in onCreate (Bundle), @param contains the values ​​provided from the savedInstanceState onSaveInstanceState (Bundle) method. Otherwise it returns null.
Most applications use onCreate (Bundle) for Activity recreation states. However, in some cases, you may need to do some work after the UI has been prepared. It is used in such cases.

onPostCreate()

OnCreate (Bundle) or onRestoreInstanceState (Bundle) are called after your activity is drawn UI.
Most applications do not need to be used.
This method is mostly used for system classes to assign initial values ​​after executing the application code.

onResume()

It is the best place for your activity to interact with the user.
onRestoreInstanceState (Bundle) is called after onRestart () or onPause () methods.
Special functions, such as animations and Camera, are used for the first time or if they are from the background.

onPostResume()

Special access functions, such as onResume (), Animations or Camera, are called after the first or repeat operation is started.
Most applications do not need to be used.
This method is mostly used for system classes to assign initial values ​​after executing the application code.

onAttachedToWindow()

Called when the screen associated with the activity is added to the screen manager.

OnPause()

Activity is sent to the background; but not when it is killed.
OnResume () is the reverse operation.
If you call B Activity while a standing A Activity exists, A Activity calls the onPause () method. A Activity 's B Activity is not created without calling onPause ().
Animation, Camera, such as the functions that will spend the CPU shutdown is done here.
Activities that have passed to onPause () status can be terminated if the system requires more memory. In this case, you can use the OnPause () içina
You may want to save when you switch. However, this process is mostly done in the onSaveInstanceState (Bundle) method.

OnStop()

Activity is now called when it cannot be viewed by the user.
OnRestart () or onDestroy () will be called.

onRestart()

The activity in the background is called again when it is shown to the user.
OnStart and onResume () will be called.

OnDestroy()

All cleaning procedures are performed here before the activity is completely eliminated.
There are two types of activity: either the finish () method was invoked somewhere or the system wanted to automatically kill the passive-standing Activity for more memory. You can use the isFinish () method to separate these two states. it will return true if it is closed with finish ().
Do not use this method to record the activity status. OnPause () or onSaveInstanceState (Bundle) methods should be used to do this. When there is no more Activity, objects that will not need to run will be shut down and cleared from memory. This provides space and performance for other applications.

onSaveInstanceState()

An Activity is called just before it is killed. This allows you to record the status of the Activity. You can reuse your saved state later on the onCreate (Bundle) or onRestoreInstanceState (Bundle) methods. Thus, no data loss occurs.
For example, the A Activity is on. So let's open B Activity and pass A Activity to passive status and send it to the background. In this case, the System can kill your A Activity at any time to make more room. In this case, if you want to return to A Acitivity from B Activity, A has retained all the current status of Actvitiy, and you should have called and used this again so that you do not lose any data. This is where you're gonna do it.

onCreateOptionsMenu()

When the options menu is displayed for the first time, it is called only once. You can define the new menu.
To update the menu every time you view it, see onprepareoptionsme (MENU).

onPrepareOptionsMenu()

The Options menu is called each time it is displayed. Menu items can be used effectively to enable or disable.

onUserInteraction()

This method is called if the user interacts with the screen in any way while your activity is standing.
Each time onUserLeaveHint () is called, this method will be called by accompanying it again.

onUserLeaveHint()

The user is called by pressing the Home button if he wants to take Activity in the foot to the background.
Calls to the phone are also received in the background if there is a standing Activity. In this case, however, onUserLeaveHint () is not called. Only when the user intentionally wants to put your Activity into the background is called.
When activity is taken into the background, if this is a user selection, onUserLeaveHint () is called just before the onPause () method is called.

onActivityResult()

Called when you want to start Activity with a mak requestCode ’. You can also move extra data next to requestCode during this startup.
You can use this way to move a number from the phone book to your Activity.

onAttachFragment()

If a fragment wants to be added to your Activity, then it is called.

onConfigurationChanged()

When you define an Activity on the Manifest file, it will tell your Activity when you type what you want to be aware of the settings changed by the system in the configChanges property (language change, font and size, change of screen orientation, etc.).
Important notice, in case the system changes are not made in the manifest, your Activity is killed and restarted. In this case, you must save the Activity status in the onSaveInstanceState (Bundle) method and then you must reach the Activity state in one of the onCreate (Bundle) or onRestoreInstanceState (Bundle) methods. Otherwise, you may experience crash conditions in your application. The reason that Activity was killed and restarted by the System is to apply the new system settings to your existing Activity.

onBackPressed()

If the user presses the back key on the device, it is called.
The general behavior is to kill the current activity. However, after override you can do any other operation.
For example, you might want to create a dialog such as ”Are you Sure to Leave?"

You can see all these override methods on this code snippet :

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    printLog("onCreate setContentView öncesi")
    setContentView(R.layout.activity_life_cycle)
    printLog("onCreate setContentView sonrası")
}

//        override fun onCreateView(name: String?, context:     Context?, attrs: AttributeSet?): View {
//        printLog("onCreateView1")
//        return super.onCreateView(name, context, attrs)
//    }

override fun onContentChanged() {
    super.onContentChanged()
    printLog("onContentChanged")
}

override fun onStart() {
    super.onStart()
    printLog("onStart")
}

override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
    super.onRestoreInstanceState(savedInstanceState)
    printLog("onRestoreInstanceState")
}

override fun onPostCreate(savedInstanceState: Bundle?) {
    super.onPostCreate(savedInstanceState)
    printLog("onPostCreate")
}

override fun onResume() {
    super.onResume()
    printLog("onResume")
}

override fun onPostResume() {
    super.onPostResume()
    printLog("onPostResume")
}

override fun onAttachedToWindow() {
    super.onAttachedToWindow()
    printLog("onAttachedToWindow")
}

override fun onPause() {
    super.onPause()
    printLog("onPause")
}

override fun onStop() {
    super.onStop()
    printLog("onStop")
}

override fun onRestart() {
    super.onRestart()
    printLog("onRestart")
}

override fun onDestroy() {
    super.onDestroy()
    printLog("onDestroy")
}

override fun onSaveInstanceState(outState: Bundle?) {
    super.onSaveInstanceState(outState)
    printLog("onSaveInstanceState")
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    printLog("onCreateOptionsMenu")
    return super.onCreateOptionsMenu(menu)
}

override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
    printLog("onPrepareOptionsMenu")
    return super.onPrepareOptionsMenu(menu)
}

override fun onUserInteraction() {
    super.onUserInteraction()
    printLog("onUserInteraction")
}

override fun onUserLeaveHint() {
    super.onUserLeaveHint()
    printLog("onUserLeaveHint")
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    printLog("onActivityResult")
}

override fun onAttachFragment(fragment: Fragment?) {
    super.onAttachFragment(fragment)
    printLog("onAttachFragment")
}

override fun onConfigurationChanged(newConfig: Configuration?) {
    super.onConfigurationChanged(newConfig)
    printLog("onConfigurationChanged")
}

override fun onBackPressed() {
    super.onBackPressed()
    printLog("onBackPressed")
}

Leave a reply:

Your email address will not be published.

Sliding Sidebar

A few words about me

A few words about me

Software engineer who likes weekend more than weekdays. Loves food and cooks good as well. Loves to travel and gym, can never spend a weekend sitting idle at home. Independent, modern man with a traditional heart. My actions speak more than words.

Social Profiles