Android Lifecycle Methods

An Android activity is one screen of the Android app's user interface. In that way an Android activity is very similar to windows i...


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:

                       

COMMENTS

Name

Android,9,Big Data,3,Cloud Apps,1,Database,1,Featured,12,Free Downloads,1,HowTo,1,IOT,7,Java,1,Linux,1,News,6,Products,1,SEO,1,TipsnTricks,5,Ubuntu,1,Unity3D,4,Windows Apps,1,
ltr
item
Tech Geek Bytes - A Blog For News, Tutorials, Free Source Code, Tips and Tricks: Android Lifecycle Methods
Android Lifecycle Methods
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikdjln0wwZNoumVUo_XdwX8yYTVpleYLW968uysWyv3updaHCEVRYNSdJwFMrrH0ibmHGbYAM9g8HwmDI485YmxaPg3Ne1fIWbvs-cSm21DUiUo8i6_TU57FtFBpRMPSC-6BHepaqM70g/s1600/i.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikdjln0wwZNoumVUo_XdwX8yYTVpleYLW968uysWyv3updaHCEVRYNSdJwFMrrH0ibmHGbYAM9g8HwmDI485YmxaPg3Ne1fIWbvs-cSm21DUiUo8i6_TU57FtFBpRMPSC-6BHepaqM70g/s72-c/i.jpg
Tech Geek Bytes - A Blog For News, Tutorials, Free Source Code, Tips and Tricks
https://techgeekbytes.blogspot.com/2017/06/android-lifecycle-methods.html
https://techgeekbytes.blogspot.com/
https://techgeekbytes.blogspot.com/
https://techgeekbytes.blogspot.com/2017/06/android-lifecycle-methods.html
true
8751912191394201619
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy