Published on

Android Lifecycle: Things to Know

Authors

After you've grasped the key components of an Android application - Android Fundamentals. Next, let's learn about Lifecycle, this will be the foundation to help you manage application resources effectively!

Every Object in a program has a Lifecycle. Some special Objects have lifecycles that need to be considered during software development.

Application Lifecycle

In each Android application, it will be represented by an Application class. However, in the default state of an Android project, you don't see the presence of this class in the Project.

Create a Kotlin Class named MyApplication that inherits from Application

Run the application and view the Logs.

Activity Lifecycle

class StandardActivity : AppCompatActivity() {
    private var viewBinding: ActivityStandardBinding? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = ActivityStandardBinding.inflate(layoutInflater)
        setContentView(viewBinding!!.root)
        Log.d("StandardActivity", "onCreate")
    }
    override fun onStart() {
        super.onStart()
        Log.d("StandardActivity", "onStart")
    }
    override fun onResume() {
        super.onResume()
        Log.d("StandardActivity", "onResume")
    }
    override fun onPause() {
        super.onPause()
        Log.d("StandardActivity", "onPause")
    }
    override fun onStop() {
        super.onStop()
        Log.d("StandardActivity", "onStop")
    }
    override fun onDestroy() {
        super.onDestroy()
        Log.d("StandardActivity", "onDestroy")
    }
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("StandardActivity", "onSaveInstanceState")
    }
    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.d("StandardActivity", "onRestoreInstanceState")
    }
    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        Log.d("StandardActivity", "onAttachedToWindow")
    }
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        Log.d("StandardActivity", "onDetachedFromWindow")
    }
    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(newBase)
        Log.d("StandardActivity", "attachBaseContext")
    }
}

Add these Logs to your Activity and observe their Logs - Android Mastery by Dan Tech

Fragment Lifecycle

Fragment is a complex category in Android Programming. It can be said that managing Fragments in Android is very difficult for newbies, even those with many years of experience. Nowadays, with the support of Jetpack Navigation or Compose. The difficulty of Fragments has been significantly reduced, however, understanding and grasping knowledge about Fragments is always necessary for an Android Engineer. To become a better person, you have to grasp knowledge that no one wants to understand.

To systematize Fragment knowledge, as well as provide a really reasonable learning path, please study the knowledge below carefully!

Fragment Concept

A Fragment is a smaller unit than an Activity, but larger than a Layout and View in Android Programming. Each Activity can contain one or more Fragments at the same time, and each Fragment can also contain many Fragments inside them. This inadvertently increases the complexity of the Android application in case we design features with too many nested Fragments.

A Fragment can be of any size depending on the programming ability of the Software Engineer. However, for easy maintenance and good application performance - I advise all Android Programmers when working with Fragments to let that Fragment be a Fullscreen Fragment, and try to minimize the cases of many child Fragments inside 1 Fragment.

How to create and use Fragments in Activity

Fragment Lifecycle

Just like Application or Activity, Fragment also has its own Lifecycle. Because Fragments are managed by Activity, each Lifecycle of the Fragment will be closely related to the Activity containing it.

List of Lifecycle callbacks in Android Fragment

class SimpleFragment : Fragment() {
    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.d("SimpleFragment", "onAttach")
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d("SimpleFragment", "onCreate")
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.d("SimpleFragment", "onCreateView")
        binding = FragmentSimpleBinding.inflate(inflater, container, false)
        return binding?.root
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.d("SimpleFragment", "onViewCreated")
    }
    override fun onViewStateRestored(savedInstanceState: Bundle?) {
        super.onViewStateRestored(savedInstanceState)
        Log.d("SimpleFragment", "onViewStateRestored")
    }
    override fun onStart() {
        super.onStart()
        Log.d("SimpleFragment", "onStart")
    }
    override fun onResume() {
        super.onResume()
        Log.d("SimpleFragment", "onResume")
    }
    override fun onPause() {
        super.onPause()
        Log.d("SimpleFragment", "onPause")
    }
    override fun onStop() {
        super.onStop()
        Log.d("SimpleFragment", "onStop")
    }
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("SimpleFragment", "onSaveInstanceState")
    }
    override fun onDestroyView() {
        super.onDestroyView()
        Log.d("SimpleFragment", "onDestroyView")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("SimpleFragment", "onDestroy")
    }
    override fun onDetach() {
        super.onDetach()
        Log.d("SimpleFragment", "onDetach")
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        Log.d("SimpleFragment", "onConfigurationChanged")
    }
}

OnConfigurationChanged

Configuration is a set of general parameters used in a running Android application, which can be listed: screen size (screenSize), screen orientation (orientation), keyboard hidden/shown (keyboardHidden), Language (locale) ..

Whenever any parameter of this Configuration changes. The Activity will automatically be re-created, pulling the re-creation of the Fragments inside it. At the same time, the callback is also returned Application.onConfigurationChanged

To proactively handle changes inside the Configuration without causing the Activity to re-create, the Programmer can add an attribute to the android:configChanges field in the Activity declaration in AndroidManifest

android:configChanges="orientation|screenSize"

It must be added that, the approach to handling Configuration Changes manually like this is a difficult and complex approach. Learning it can waste time for newcomers because they are lacking a lot of knowledge about Android programming.

In this area of ​​knowledge, Danh will stop at providing knowledge without going too deep into examples to avoid discouraging you.

For reference source code, please visit my GitHub here