DEV Community

Cover image for Android activity and lifecycle
Tristan Elliott
Tristan Elliott

Posted on • Updated on

Android activity and lifecycle

Introduction

  • This series is going to be dedicated to the basic to Android development. Join me and let us try to build and understand some cool stuff. I used the Android documentation for all the information in this post.

Quick Note: any time I reference system I am talking about the Android system, which is a modified Linux kernel system.

What is an activity?

  • Consisting of an .XML file for the UI and a Java file to handle the logic, an activity is a crucial part of any android app. Unlike a normal Java application, an Android app does not have a main method to act as a starting point. Instead the Android System(modified Linux kernel system) uses activities to run code. The code that is being run is specified to run in certain stages of the activitie's lifecycle.

What is the activity lifecycle?

  • As the user navigates through individual activities or navigates from one activity to another. The activities go through a lifecycle, as they go through this lifecycle they trigger specific callback methods at points in the lifecycle called stages. These callback methods dictate how the activate behaves when the certain stage is reached.

  • Each activity and its lifecycle are stored on a backstack which operates on the traditional stack logic of first in, last out. It is thanks to this backstack that we are able to use the back button to transition through our previous activities.

  • To handle the transitions between states in the activity lifecycle, the Activity class provides us with 7 methods.

1) onCreate() : this method is mandatory and must be implemented when creating an activity. It is called when the activity is first created, this method is where we should create views(UI) and bind data to lists. It also provides us with a Bundle variable which is the activities previous state, if there is any.

2) onStart() : this method is called when the activity becomes visible to the user. It is a good place to begin drawing visual elements and running animations.

3) onResume() : this method is called when the activity will start interacting with the user. At this point the activity is at the top of the backstack. The activity will stay in this state until something happens to take focus away from the activity.

4) onPause() : always called after onResume() and when the user is no longer actively interacting with the activity but the activity is still visible on the screen.

5) onStop() : this is called when the activity is no longer visible to the user. This may happen because the activity has been destroyed or a new activity is starting.

6) onRestart() : called when the current activity is being redisplayed to the user. This callback is followed by a call to the onStart() method.

7) onDestroy() : is called before the activity is destroyed. The system invokes this callback either because, the user dismisses the activity, or there is a configuration change(device rotation).

References

Activity Documentation

Intro to activities

Activity lifecycle video

Understanding Activities LifeCycle

Backstack

Conclusion

  • Thank you for taking the time out of you day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.
  • Also make sure to checkout my YouTube channel for more programming tutorials.

Top comments (0)