24. Activity Lifecycle, an example of a state transition with two Activities

Lecture



In this lesson:

- we study state change on the example of two Activities

In the last lesson, we looked at what states the Activity goes through during its existence and which methods are invoked. But we only saw the Activity in the Resumed state (i.e. it is visible, and it is in focus). In this lesson, using the example of two Activities, we will try to understand in which case the Activity can remain in the Stopped state, i.e. not visible and out of focus, but exists in memory.

Let's create   project :

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

In the main.xml we write the following:

  <? xml version = "1.0" encoding = "utf-8"?> 
<LinearLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
android: orientation = "vertical"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent">
<Textview
android: layout_width = "fill_parent"
android: layout_height = "wrap_content"
android: text = "@ string / hello">
</ TextView>
<Button
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "Go to Activity Two"
android: id = "@ + id / btnActTwo">
</ Button>
</ LinearLayout>

The “Go to Activity Two” button will trigger the second Activity.

Let's open MainActivity.java and write all the methods there , this time, including onRestart , and in the methods we write an entry to the logs . We also describe and find the button , assign a handler to it. We are not writing anything in the onClick method.

package ru.startandroid.develop.p0241twoactivitystate;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

final String TAG = "States";

Button btnActTwo;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnActTwo = (Button) findViewById(R.id.btnActTwo);
btnActTwo.setOnClickListener(this);

Log.d(TAG, "MainActivity: onCreate()");
}

@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "MainActivity: onRestart()");
}

@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "MainActivity: onStart()");
}

@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "MainActivity: onResume()");
}

@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "MainActivity: onPause()");
}

@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "MainActivity: onStop()");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "MainActivity: onDestroy()");
}

@Override
public void onClick(View v) {
}
}

What methods and in what order are performed during the work of one Activity, we saw in the last lesson. Now we are interested in the behavior with two Activities, so we create a second Activity. Let's call it ActivityTwo . We recall past lessons: we need to create a class with the same name and with the superclass of android.app.Activity, and register a new Activity in the manifest file. You also need to create a layout file, call it two.xml and fill it with this code:

  <? xml version = "1.0" encoding = "utf-8"?> 
<LinearLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
android: orientation = "vertical"
android: layout_width = "match_parent"
android: layout_height = "match_parent">
<Textview
android: id = "@ + id / textView1"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "This is Activity Two">
</ TextView>
</ LinearLayout>

Just a textview with text so that it is clear that this is ActivityTwo.

Create a class. ActivityTwo.java code:

package ru.startandroid.develop.p0241twoactivitystate;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class ActivityTwo extends Activity {

final String TAG = "States";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
Log.d(TAG, "ActivityTwo: onCreate()");
}

@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "ActivityTwo: onRestart()");
}

@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "ActivityTwo: onStart()");
}

@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "ActivityTwo: onResume()");
}

@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "ActivityTwo: onPause()");
}

@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "ActivityTwo: onStop()");
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "ActivityTwo: onDestroy()");
}
}

Do not forget to add an entry about ActivityTwo to the manifest . And now we can add the code of the onClick method in MainActivity.java, registering there a call ActivityTwo

@Override
public void onClick(View v) {
Intent intent = new Intent(this, ActivityTwo.class);
startActivity(intent);
}

(add only underlined code).

The log filter should have remained from the last lesson. We use it. If not, create a filter for the States tag.

All save and proceed to the tests.

Step 1. Run the application. Appeared MainActivity.

  24. Activity Lifecycle, an example of a state transition with two Activities

Logs:

MainActivity: onCreate ()
MainActivity: onStart ()
MainActivity: onResume ()

Everything, as well as last time - three methods are caused. The activity goes through the states Stopped, Paused and remains in the Resumed state.

Step 2. Click the “Go to Activity Two” button on the screen and ActivityTwo appears.

  24. Activity Lifecycle, an example of a state transition with two Activities

Logs:

MainActivity: onPause ()
ActivityTwo: onCreate ()
ActivityTwo: onStart ()
ActivityTwo: onResume ()
MainActivity: onStop ()

Let's figure it out. The call to MainActivity.onPause means that the MainActivity loses focus and goes to the Paused state. It is then created (onCreate), displayed (onStart) and receives focus (onResume) ActivityTwo. Then ceases to be visible (onStop) MainActivity. Note that onDestroy for MainActivity is not called, which means it is not destroyed. MainActivity remains in memory, in the Stopped state. And ActivityTwo is in a Resumed state. It can be seen and it is in focus, you can interact with it.

Step 3. Click the Back button on the emulator. We are back in the MainActivity.

  24. Activity Lifecycle, an example of a state transition with two Activities

Logs:

ActivityTwo: onPause ()
MainActivity: onRestart ()
MainActivity: onStart ()
MainActivity: onResume ()
ActivityTwo: onStop ()
ActivityTwo: onDestroy ()

ActivityTwo.onPause means that ActivityTwo loses focus and goes into Paused state. MainActivity should now recover from Stopped status. At the end of the last lesson I wrote: “the onRestart method is called before the onStart method, if the Activity is not created from scratch, but is restored from the Stoped state” - this is our case, the MainActivity was not destroyed by the system, it hung in memory. Therefore, MainActivity.onRestart is called. Then the methods MainActivity.onStart and MainActivity.onResume are called - means MainActivity has passed into the state Paused (displayed) and Resumed (received focus). Well, calling the onStop and onDestroy methods means that ActivityTwo was transferred to the status Stopped (lost visibility) and was destroyed.

Step 4. Click Back again and our application is closed.

  24. Activity Lifecycle, an example of a state transition with two Activities

Logs:

MainActivity: onPause ()
MainActivity: onStop ()
MainActivity: onDestroy ()

Logs indicate that the MainActivity has passed into the Paused, Stopped state and has been destroyed.

If it is not clear from the first time, try to run the algorithm several times and check the scheme from the last lesson of the lesson. It is quite visual and will help to understand. Try to paint the entire scheme on paper and draw a change in the status of the Activity. I will also give here a diagram of steps for clarity.

  24. Activity Lifecycle, an example of a state transition with two Activities

We saw that the Activity is not necessarily destroyed when it is not visible, but can remain in memory. In this regard, I think, the question probably arose: why at step 2 MainActivity disappeared from the screen, but it remained to hang in memory and was not destroyed? Indeed, at Step 3, ActivityTwo was destroyed after it disappeared from the screen. And in step 4, the MainActivity was eventually destroyed. Why was step 2 an exception?

We will talk about this in the next lesson, because This and so turned out too abstruse. But the topic is very important and one of the key to understanding how Android works.

If something did not work, write to kamenty.

In the next lesson:

- a little theory on Task
- fix the Activity in the Paused state


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Mobile Programming (Android IOs)

Terms: Mobile Programming (Android IOs)