Google+

Android4Beginners.com

Apps development for Android step by step

Lesson 0.9: Android app life from onCreate to onDestroy – what happens when we start app (and what are “activities”)

Android apps are built from activities. Activity is a set of actions visible on the screen (usually it takes whole screen, but it could be in a window). The app could have many activities. For instance a music player has a main menu activity, album list activity, song play activity etc. So activity is a combination of user interface and some functions.

In the previous lesson we opened MainActivity Java file. This activity is presented to a user when you start an app. Our simple Hello world! app has only one activity, but later we will learn how to add more activities and switch between them.

Every activity has its lifecycle. So it knows what to do at start and what to do when it finishes. There are three states of life: running (called also resumed), paused and stopped. When activity switches from one state to another it sends notification (lifecycle callback), so you could react to it.

The life of activity begins with onCreate() callback method. At this stage it isn’t yet visible to a user and you have a time to prepare everything, like decide what would be presented on the screen. Then is called onStart() method – this is a moment when activity is shown to user so everything should be ready.

When you switch to another activity (or app) onStop() is called. This is a good time to save a user data added when activity was running. Though activity is stopped it’s still in device memory. You could come back to it and continue what you were doing. So it’s a different situation than starting activity that’s why there is separate method onRestart().

That’s not all yet. If you put away a device for a while it would go to sleep. The activity is still on the screen, but it’s not activity – this would trigger onPause() method. When you touch a screen to wake a device onResume() is called. And finally when the activity is stopped and device memory it uses is needed for another activities it would be “killed” what’s confirm by onDestroy().

It could seem a bit complicated, but there are two good messages:

  • firstly Google created nice graphics showing activity lifecycle and methods related to every transition so it easier to understand it (see tem below),
  • secondly in most cases you would need to take care only about things that happen at the beginning so at onCreate(). Android system takes care about the rest.
This is a whole Android activity lifecycle illustrating all states and callback methods (Photo credit by Google)

This is a whole Android activity lifecycle illustrating all states and callback methods (Photo credit by Google)

This is the same Android activity lifecycle, but showed as a pyramid. Usually the most actions we have to do at onCreate() method (Photo credit by Google)

This is the same Android activity lifecycle, but showed as a pyramid. Usually the most actions we have to do at onCreate() method (Photo credit by Google)

Now it’s a good time to look again at our Hello world! app and callback methods inside it. You could learn how to open MainActivity file from the previous lesson.

This is an original code of MainActivity:

We will not yet go deeply into Java, but have a look at two lines. Line 7 states that MainActivity is an activity (every activity has to extend Activity class, what would be explained later).

And line 10 starts onCreate() method. When the app is started just one important thing happens – the layout from activity_main.xml is displayed on a screen. For showing specific layout file we use:

Letter R refers to resources, so data collected in res folder mentioned in previous lesson. Layout refers to layout subfolder of res. And finally we have to give a name of a layout file we want to display (but without its extension).

There is one command before:

To make it simple it recreates the state of the app when you change orientation of device. When you start the app savedInstanceState is empty so nothing happens, but when you rotate a smartphone or tablet  Android saves the state of activity to so called Bundle and then reload it. So if you modify something on the screen when it was vertical it would be visible on horizontal even though layout could be different. Fortunately, we don’t have to bother about it, just always add this line to onCreate().

As you can see there are not any other callback methods (like onStop or onPause) in the code. It means that system would take care about them on its own.

Summary: We need activities to interact with the user. Activity has a lifecycle from onCreate() to onDestroy(). Especially the first method is important as it allows us to decide what would happen when user starts an activity. When users click an app icon it starts so called main activity (if you have a few activities, you declare which one is main – in a new project it’s called MainActivity). At least it should display layout using setContentView() method.

In the next lessons we try to understand main Java concepts, like objects and classes.

Previous:
Next: