DEV Community

loading...

Introduction to Android Lifecycle

Hein Khant Zaw
I am a Computer Science Undergraduate Student from the University of Information Technology Yangon. I'm from Myanmar(Burma), currently living in Yangon.
・2 min read

The android activity lifecycle includes the following callbacks: onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy().

Activity Lifecycle

onCreate()

The lifecycle of an android activity begins here. This is where any layout resources (Buttons, textViews, editText, etc.) and background threads are initialized.

onStart()

Called right after onCreate(). In this state the activity becomes visible to the user.

onRestart()

Called after an activity has been stopped. This call reverts the activity to its visible lifetime.

onResume()

Called when the user returns to an activity after briefly exiting it. The app stays in this state until something happens to take focus away from the app. Such an event might be, for instance, receiving a phone call, the user’s navigating to another activity, or the device screen’s turning off.

onPause()

Called when the user leaves the current activity to another activity or leaves the app for a while. It indicates that the activity is no longer in the foreground and certain processes that are not needed while the activity is not in the foreground will be stopped.

onStop()

Called when the activiy becomes invisible to the user. The activity and its resources may still be running in the background even though it is not visible. After onStop() is called, the activity will call onRestart() if it needs to be loaded again.

onDestroy()

The end of the activity lifecycle. This activity is used to stop all resources and tasks started in onCreate(). After onDestroy() is called, the activity will call onCreate() if it needs to be loaded again.
The system invokes this callback either because:

  1. The activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or
  2. The system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)

You can learn more about android life cycle here !

Discussion (4)

Collapse
pouya profile image
Pouya

Thank you, if you use single activity and multiple fragments, you may need this picture (attached), I found it at web.
activity/fragment lifecycle

Collapse
heinkhantzaw profile image
Hein Khant Zaw Author

Thank you bro!

Collapse
arashnrim profile image
Arash

Thank you for sharing! I think this is definitely something useful to keep, even if not needed for now, so that it'll be better to understand developing things for Android. 🙌

Collapse
heinkhantzaw profile image
Hein Khant Zaw Author • Edited

You are welcome bro!
If you want to learn android basic, you can also check this repo from github!

GitHub logo HeinKhantZaw / HuaweiAndroidTraining

Codes from Huawei Android Training

HuaweiAndroidTraining

Codes from Huawei Android Training

Getting Started

  1. Download as zip file.

github repo

You can also clone by using git

git clone

  1. Open Android Studio > File > Open

Android studio

  1. Choose a project from the folder.

Choosing project

  1. Run the project!

For future sessions

As I'll be adding more example projects after future sessions, you can git pull or re-download the repo But I recommend to use git because you don't need to download again and again every time I upload new code You can learn more about git here!

Here is the command of pulling the updated project.
git pull
This command pull the updated files and folder from the repo in github to your local repo in your laptop.

Details about each project

There's a README.md in every project. If you're interested, you can find more about the project in those README.md!

Day 1

Day 2