Activity Lifecycle Made Easier in Android: Part 1

Roseakoth
4 min readFeb 16, 2020

--

An activity is the single screen in Android. When you start or open your android application, it will undergo various states and that is called Android Activity Life Cycle. Android apps — more specifically Android activities — go through a number of stages from when they’re first loaded to when they get closed down.

These are handled as “events” inside your code when the user changes your app’s state in some way: by launching it, rotating it, responding to a notification or switching to another task and they should be able to continue using the app seamlessly after such an event.

To provide good user experience, you should know how to manage activity lifecyles. Whenever a transition happens in your app the system notifies you via a lifecycle callback method.

To help me explain how activity lifecycles work, i’ve defined activity lifecycle callbacks and what you should do in each callback depending on the type of application you want to make.

Here is an illustration of a simplified activity lifecycle diagram.

Activity Lifecycle diagram

Activity Lifecycle Callbacks

  1. onCreate: This is called the first time the activity starts and is therefore only called once during the lifecycle of an activity. The activity is not yet visible and you can’t interact with. In this callback you should:
  • Inflate the activity’s user interface whether findViewById or databinding.
  • Do any other initializations that only happens once during activity lifetime.

2. onStart : This is triggered when the activity is about to become visible. It can be called multiple times as the user navigates away from the activity and then back. Examples of the user “navigating away” are when they go to the home screen, or to a new activity in the app. At this point, the activity is not interactive. In onStart you should:

  • Start any sensors, animations or other procedures that need to start when the activity is visible.

3. on Restart: It is called after your activity is stopped, prior to start.

4. onResume: This is triggered when the activity is in the foreground(the activity is on screen) or has focus and the user can interact with it. Here you should:

  • Start any sensors, animations or other procedures that need to start when the activity has focus (the activity the user is currently interacting with).

5. onPause: This method is called as soon as the activity loses focus and the user can’t interact with it. An activity can lose focus without fully disappearing from the screen (for example, when a dialog appears that partially obscures the activity). Here you should:

  • Stop any sensors, animations or other procedures that should not run when the activity doesn’t have focus and is partially obscured.
  • Keep execution fast. The next activity is not shown until this completes.

6. onStop: It is called when you can no longer see the activity. Here you should:

  • Stop any sensor, animations or other procedures that should not run when the activity is not on screen.
  • You can use this to persist (permanently save) data.
  • Stop logic that updates the UI(User Interface). This should not be running when the activity is off-screen. It’s a waste of resources.
  • There are also restrictions as soon as the app goes into the background, which is when all activities in your app are in the background(When activity is fully offscreen).

7. onDestroy: It is called once when the activity is fully destroyed. This happens when you navigate back out of the activity (as in press the back button), or manually call finish(). It is your last chance to clean up resources associated with the activity. Here you should:

  • Tear down or release any resources that are related to the activity and are not automatically released for you. Forgetting to do this could cause a memory leak! Logic that refers to the activity or attempts to update the UI after the activity has been destroyed could crash the app!

This explains how your activities will run and why certain elements in your code need to be there. Hopefully, you now have a bit more understanding of how things are operating behind the scenes and what is meant by the term Android activity lifecycle.

In my next post i will be looking at fragments and its lifecycles. Stay tuned.

--

--

Roseakoth
Roseakoth

Written by Roseakoth

Frontend developer. Technical writer. Shark mindset.

No responses yet