Activity And Fragment Lifecycle in Android

Activity And Fragment Lifecycle in Android

What does lifecycle mean actually? 🤔

You alive? Oh! you're 🪽

Just like the human lifecycle goes through different stages of life from birth to death. Android activity/fragment has the same. It has some methods that let us know the current situation the activity/fragment is currently in.

Activity Lifecycle

Activity lifecycle consist of six(6) main stages :

  1. onCreate()

  2. onStart()

  3. onResume()

  4. onPause()

  5. onStop()

  6. onDestroy()

In addition to this, we have also one(1) transitional callback

  • onRestart()

Here's the official graphic of the typical activity lifecycle:

Activity lifecycle graphic

Explanation time :

Crazy Talking GIF

  1. onCreate():

    This method is called when your activity is being created. If you can remember, this is where we inflate our screen layout. An ideal place to initialize startup logic that happens only once for the entire life of the activity, like instantiating ViewModel!

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

    This method is called when the activity becomes visible to the user. However, the activity is not ready yet for user interaction.

    This is the best place to perform operations that should happen every time the activity becomes visible such as updating the UI, and preparing data before the activity comes to the foreground.

  3. onResume():

    Finally, the activity is ready for user interaction, such as user input, button click, starting a camera preview, etc.

  4. onPause():

    This callback is triggered when the current activity is about to lose focus**(Still visible but not in the foreground)**. This might happen in various situations like a dialog box appearing, or a phone call from the notification bar.

  5. onStop():

    Finally, the activity is no longer in the foreground. It's a good time to release any resources, save any persistent data, and clear camera preview i.e. stop stuff that is not needed when that screen is not visible.

  6. onDestroy():

    It's time to go 🥲. The activist is about to destroy. This typically happens for two main reasons

    1. Device Configuration Changes: Such as screen rotation, theme change, system language change, etc. In such cases, the system may destroy and recreate the activity to apply the new configuration.

    2. Explicit Finish: The activity's finish() method is explicitly called by the user or the system to free up resources to respond to high-priority process needs.

  7. onRestart():

    This method is called after the onStop() when the activity is being restarted from a stopped state. It's invoked when the user navigates back to the activity before it is destroyed (Checking WhatsApp and back to that app😉)

Halfway 🎉, take some time to digest, cz if you do 89% is done!

No Rush Take Your Time Jamison Fields GIF

Fragment Lifecycle

Fragment lifecycle consists of total of twelve(12) stages, which are:

  1. onAttach()

  2. onCreate()

  3. onCreateView()

  4. onViewCreated()

  5. onActivityCreated()

  6. onStart()

  7. onResume()

  8. onPause()

  9. onStop()

  10. onDestroyView()

  11. onDestroy()

  12. onDetach()

Here's the official graphic of the typical fragment lifecycle:

Note: onViewCreated() is not present here

Explanation time :

Crazy Talking GIF

  1. onAttach():

    This method is called when the fragment is first attached to its context (usually an activity). It's where you can perform initial setup that requires parent context 😉

  2. onCreate():

    This method is Called when the fragment is being created.

  3. onCreateView():

    Called to create the fragment’s view means, we Inflate the fragment’s layout here.

     override fun onCreateView(
         inflater: LayoutInflater, container: ViewGroup?,
         savedInstanceState: Bundle?,
     ): View? {
         return inflater.inflate(R.layout.home_layout, container, false)
     }
    
  4. onViewCreated():

    It's Called immediately after onCreateView(). Here we can perform additional setup on the view (like setting up adapters).

  5. onActivityCreated():

    It's invoked when the activity’s onCreate() method has been completed. It indicates that the activity and the fragment’s view have been fully created.

  6. onStart():

    Now The fragment becomes visible to the user. However, the fragment is not ready yet for user interaction (Deja vu? means you're paying attention).

  7. onResume():

    The fragment is now in the foreground and the user can interact with it.

  8. onPause():

    Called when the fragment is no longer in the foreground but still visible. it is best to pause ongoing tasks that should not run while the fragment is not active.

  9. onStop():

    The fragment is no longer visible to the user. Release resources that are not needed while the fragment is not visible.

  10. onDestroyView():

    This method is called when the view hierarchy associated with the fragment is being removed. Clean up resources related to the view.

  11. onDestroy():

    Called to do the final cleanup before the fragment is destroyed.

  12. onDetach():

    Called when the fragment is detached from its context. Clean up any references to the activity to avoid memory leaks.

Further reference:

Difference between onCreateView and onViewCreated in Fragment

📝Note: The best way to learn is to get your hands dirty, so Override these methods on your project and see for yourself 😉🪽

Connect with me {here} for help. Happy coding, and until next time, Sayonara! 👋🏽