Android Lifecycle Methods

i.jpg

An Android activity is one screen of the Android app’s user interface. In that way an Android activity is very similar to windows in a desktop application. An Android app may contain one or more activities, meaning one or more screens. The Android app starts by showing the main activity, and from there the app may make it possible to open additional activities.
Activity Life Cycle

Any Android activity goes through a certain life cycle during its life inside the Android app. This life cycle is illustrated here:

                           
When an Android app is first started the main activity is created. The activity then goes through 3 states before it is ready to serve the user: Created, started and resumed.

If the main activity can open any other activities (screens) these activities will go through the same 3 states when they are opened.

If an activity A opens another activity B, then activity A will be paused. This means that activity A goes into the paused state. When the user clicks the back button and returns to activity A, activity A returns to the resumed state.

If the user returns to the home screen of the Android device, all activities will be paused and then stopped. If the user then returns to the app, the activities will go through the started and then resumed states. If the Android device needs the memory that the Android app occupies in the device’s memory, then it may completely destroy the app, meaning the Android activities go into the destroyed state.

Note that the states always follows the arrows in the diagram. An activity cannot jump from created to resumed directly. An activity will always go through created, started and resumed in that sequence. An activity may change from resumed to paused an back, and from paused to stopped and back to started, but never from stopped directly to resumed.

Activity Life Cycle Methods

All activities in your Android apps are represented by an activity class. These activity classes are subclasses of android.app.Activity. The Activity class contains a set of methods that corresponds to the life cycle states an activity can be in. These methods are:

onCreate()
onStart()
onRestart()
onResume()
onPause()
onStop()
onDestroy()

When activity is to transition into one of the life cycle states, the corresponding life cycle method is called on the Activity subclass instance. When the life cycle method finishes the activity is assumed to be in the new life cycle state. For instance, once the onCreate() life cycle method returns, the activity is assumed to be in the created state.

As mentioned earlier, all Android activities are subclasses of the Activity class. The Activity class contains the above listed life cycle methods. If your activity class needs to do something special when it changes life cycle state, you can override the corresponding life cycle method in your Activity subclass. In fact, your activity classes will pretty much always need to override at least one of these life cycle methods. For instance, to initialize the UI, or load / save data etc.

A small note: The onRestart() life cycle method is called when the activity transitions from stopped to started. After onRestart() the life cycle method onStart() is called, and the activity is considered started again.

Here is an example Activity subclass called MyActivity which overrides all of the above life cycle methods:

                               

Notice how each of the life cycle methods call the corresponding method in the superclass. If your Activity subclass overrides one of the life cycle methods, it must call the corresponding method in the superclass inside the overridden method.

Creating an Activity

When you create a new Android project in Android Studio the project will contain an Android Activity class already (unless you choose that is should not create an activity).

An activity is a subclass of the Android class android.app.Activity . Here is an example of how an Activity subclass could look:

                        

This Activity subclass does not do anything interesting. It just extends android.app.Activity .

When you create a blank activity in Android Studio the Activity subclass which Android Studio generates for you looks like this:

                      

Notice how this Activity subclass overrides the life cycle methods onCreate(), and the two other methods onCreateOptionsMenu() and onOptionsItemsSelected() . All of these methods will be covered in more detail later, so I won’t say more about them here. What is important to know at this time is that the Activity subclass overrides methods inherited from the Activity class in order to do anything interesting.

Creating an Activity in Android Studio

When you create a new project in Android Studio, the new project dialog will ask you if you want to create an activity as part of the project. If your app only needs a single activity (a single screen), you do not need to create more Activity subclasses.

However, if your app needs more than one activity, you can add an activity to your project via the File menu, by choosing the “New…” menu item. Here is where you find that menu item:

                                 

In the menu that opens, click on the “Activity” menu item, and choose what kind of activity you want to create. Here is how that looks in Android Studio:

                                    

Activity XML Layout Files

An activity may have an associated XML layout file. The XML layout file specifies how the GUI components in the activity are to be rendered relative to each other. In the screenshot below I have marked the activity class and its associated layout XML file in Android Studio’s project browser:

                       

About Us

TechGeekBytes is a free Technology resource site for Beginners. TechGeekBytes was founded in 2016 by Open Hardware Days Pvt Ltd. The main goal of this site is to provide quality tips, tricks, hacks, and other Technology resources that allows beginners to improve their skill(s).