Activity Lifecycle in Android Applications

Learn about different stages of activity lifecycle

Sung Park
Sketchware
4 min readMar 14, 2017

--

If you have experience creating a multi-paged application on Sketchware, you’re probably already familiar with the concept of Intent and Activity. If you haven’t, I recommend you to read this tutorial on creating a Memo application before reading this article.

Lifecycle events were added to Sketchware in version 1.2 to help users to have more control and create more complicated applications. By implementing these new features, users are now able to manipulate how the application would behave at different stages of the lifecycle.

Activity Lifecycle

Every screen on Android applications has a lifecycle. When you open any application, some kind of screen will welcome you. Behind the scenes, this welcome screen had to be created before it could deliver its contents to your eyes. Without you even realizing, the screen or activity went through few stages in its lifecycle. Pretty interesting, huh? Today, we’ll talk about what Lifecycle means in Android programming.

Here is a broad definition of Lifecycle from Android Documentation.

As a user navigates through, out of, and back to your app, the Activity instances in your app transition through different states in their lifecycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.

The scene or the screen you’re currently on is known as an Activity. Below is a common way to transition to another screen using Intent in Sketchware:

Each Activity provides a core set of six callbacks or functions: onCreate(), onStart(),onResume(), onPause(), onStop(), and onDestroy(). The system invokes each of these callbacks as an activity enters a new state.

After we started adding more features into Sketchware, we realized users would need more lifecycle events to fully have control over the application. For example, when you play a music through MediaPlayer in Sketchware, the music continues to play even after you exit the application by pressing the home button. Stopping the music was impossible unless you force closed the app, since only one lifecycle event onCreate() was available at the time.

To resolve this issue, we added few more lifecycle events. Note that we didn’t add every lifecycle events. We believed these would be enough to suffice the need for now.

onCreate

Simply put, this event gets fired only once when the activity is started. This is a good place to initialize any data such as variables or lists. In Sketchware, this event is provided by default. You’re probably already very familiar with this event since this event has been here since Sketchware was created.

default lifecycle event provided

onStart

Once the activity enters the “started” state, the activity becomes visible and interactive. Behind the scenes, this is where the application maintains any code related to the User Interface.

onPostCreate

This event is called when activity start-up or onStart is complete.

onResume

This event is called whenever the user returns to the activity after leaving the activity — such as receiving a call, pressing the home button, turning off the screen, or transitioning to another activity.

onPause

The activity calls this method as the first indication that the user is leaving your activity.

onStop

When your activity is no longer visible to the user. This is usually a good point to pause any ongoing process, such as MediaPlayer or SoundPool.

onDestroy

Called before the activity is destroyed. This is the final call that the activity receives.

Here is a more detailed flow chart provided from the documentation:

Conclusion

We learned about different stages of lifecycle in Android applications. These may not come in handy yet, but as Sketchware progresses and adds more blocks and features, these events will be very important to utilize.

Happy Coding! :-)

If you enjoyed this post, please press the 💚 below so others can find this article too!

--

--