[Android] Beginner Tutorials – Lesson 24. Activity Lifecycle, example about changing states with two Activities

Translated by Taras Leskiv (http://android-by-example.blogspot.com/)

In the previous lesson we have looked through which states does Activity pass and which method are invoked during its lifetime. But we have seen an Activity only in the Resumed state (it means Activity is visible and focused). In this lesson, using two Activities, we will try to understand in which cases Activity can remain in the Stopped state (it is not visible and not focused, but it exists in memory).

Let’s create a project:

Project name: P0241_TwoActivityState
Build Target: Android 2.3.3
Application name: TwoActivityState
Package name: ru.startandroid.develop.p0241twoactivitystate
Create Activity: MainActivity

Write the following in main.xml:

“Go to Activity Two” button will invoke the second Activity.

Open MainActivity.java and write there all methods, including onRestart this time, and write log entries inside these methods. We also find and define the button and assign a listener to it. Do not write anything inside onClick method for now.

In the previous lesson we have seen which methods and in which order are invoked while working with one Activity. Now we are interested in behaviour having two Activities, that’s why we create the second Activity. Let’s name it ActivityTwo. Remember the previous lessons: we have to create a class with the same name which is a subclass of android.app.Activity and register the new Activity inside the manifest-file. We also need to create a layout-file, name it two.xml and fill it in with the following code:

We have just a TextView with some text to be clear that this is ActivityTwo.

Create a class. ActivityTwo.java code:

Don’t forget to add the record about ActivityTwo to the manifest. And now we can add the onClick method code to MainActivity.java, invoking ActivityTwo there

(add only the underlined code).

Log filter should remain from the previous lesson. We will use it. If not, create the filter by the tag States.

Save everything and run the application.

Step 1. Run the application. MainActivity appeared.


Everything as the previous time – three methods are invoked. Activity goes through Stopped, Paused states and remains in the Resumed state.

Step 2. Press “Go to Activity Two” button and ActivityTwo will appear.


Let’s look through it. MainActivty.onPause invocation means that MainActivity loses its focus and goes to Pause state. After this, ActivityTwo is created (onCreate),  displayed (onStart) and receives focus (onResume). After this MainActivity is not visible any more (onStop). Note, that onDestroy is not invoked for MainActivity and it means it is not destroyed. MainActivity remains in memory in the Stopped state. And ActivityTwo – is in the Resumed state. It is visible and focused, we can interact with it.

Step 3. Press the Back button on the emulator. We’ve returned to the MainActivity.


ActivityTwo.onPause means that ActivityTwo loses its focus and goes to the Paused state. MainActivity now has to be restored from the Stopped state. In the end of the previous lesson I’ve written: “onRestart method is invoked before the onStart method, if Activity is not created from scratch, but restored from the Stopped state” – this is exactly our case, MainActivity hasn’t been killed by the system, it was still in memory. That is why MainActivity.onRestart is invoked. After this MainActivity.onStart and MainActivity.onResume are invoked – it means MainActivity went to Paused state (was displayed) and Resumed state (gained focus). And onStop and onDestroy methods invocation means that ActivityTwo changed its state to Stopped (was not visible any more) and has been destroyed.

Step 4. Press back once again and the application is closed.


Logs show that MainActivity changed its state to Paused, Stopped and has been destroyed.

If it is not clear from the fis time, try to repeat the algorithm for several times and check it with the diagram from the previous lesson. It is quite obvious and it will help you to get through. Try to draw the whole diagram on the sheet of paper and changes of the Activity state. I will also display the sequence of steps here to make it obvious.


We found out that Activity is not necessarily destroyed when it is not visible and can be stored in RAM. Looking through this, I guess a question has appeared: why in the step 2 MainActivity disappeared from the screen, but was left in memory and was not destroyed? But in the step 3 ActivityTwo was destroyed just after it disappeared from the screen. And in the step 4 in the end MainActivity was destroyed. Why is step 2 exceptional?

We will talk about this in the next lesson, as this lesson is already quite complicated. But the topic is extremely important and is one of the key concepts of the Android operating system.



Share this Post :

No comments yet.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *