Lesson 24. Activity Lifecycle, an example of changing states with two Activity

Lesson 24. Activity Lifecycle, an example of changing states with two Activity


In this lesson:

– we study the change of state on the example of two Activity

In the last lesson we looked at which status passes Activity during its existence and which methods thus called. But we only saw Activity in the state Resumed (That is, it is visible, and it is in focus). In this lesson, for example two Activity let’s try to understand in which case Activity may remain in the state StoppedThat is 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: en.startandroid.develop.p0241twoactivitystate
Create Activity: MainActivity

In main.xml we write the following:



    
    
    
 

The Go to Activity Two button will call up the second Activity.

Open MainActivity.java and write everything there methodsThis time including onRestart, And in methods prescribe an entry in logs. We also describe and find button, We assign it handler. In the method onClick as long as we don’t write anything.

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) {
  }
}

We saw in the past lesson what methods and in what order were performed when using one Activity. Now we are curious about the behavior at two Activity, so create a second Activity. let’s call it ActivityTwo. We remember past lessons: it is necessary create a class with that name and with superclass android.app.Activity, and register a new Activity in manifesto-File. We also need to create a layout file, call it two.xml and fill it with this code:



    
    

Just a TextView with text to make it clear that it’s ActivityTwo.

Let’s 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()");
  }
}

Be sure to add a post about ActivityTwo in manifesto. And now we can add the method code onClick in MainActivity.java, registering an ActivityTwo call there

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

(Only add lines 3 and 4).

The log filter had to be left over from the last lesson. We use it. If not, create a filter by tag States.

We will save everything and start testing.

Step 1. Run the program. MainActivity appears.

List:

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

Everything, like last time, is called three method. Activity passes through the Stopped, Paused states and remains in the Resumed state.

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

List:

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

Let’s figure it out. Calling MainActivity.onPause means that MainActivity loses focus and goes into Paused state. It is then created (onCreate), displayed (onStart) and received the focus (onResume) of ActivityTwo. Then (onStop) MainActivity ceases to be visible. Note that onDestroy is not called for MainActivity, so it is not destroyed. MainActivity remains in memory, in the Stopped state. And ActivityTwo is in Resumed state. It is visible and in focus, and can be interacted with.

Step 3. Click the Back button on the emulator. We’re back at MainActivity.

List:

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

ActivityTwo.onPause means that ActivityTwo loses focus and becomes Paused. MainActivity must now be restored from Stopped status. At the end of last lesson, I wrote: “The onRestart method is called before the onStart method, if Activity is not created from scratch and restored from Stopped state” – this is just our case, MainActivity was not destroyed by the system, it hung in memory. Therefore, it is called MainActivity.onRestart. The following methods are called MainActivity.onStart and MainActivity.onResume – so MainActivity went into Paused and Resumed. Well, calling the onStop and onDestroy methods means that ActivityTwo has been switched to Stopped and lost.

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

List:

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

The list shows that MainActivity went into Paused, Stopped and was destroyed.

If the first time is not clear, try to chase the algorithm several times and check with scheme from the last lesson. It is quite visual and will help to understand. Try to paint the whole scheme on paper and draw a change in Activity statuses. I will also outline the steps here for clarity.

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

We’ll talk about this in the next lesson, because this one came out too arrogant. But the topic is very important and one of the key to understanding the principles of Android.

If something does not work, write in the comments.

In the next lesson:

– a little theory on Task
– Fix Activity in Paused state




Discuss in the forum [74 replies]

Leave a Comment