You get a bonus - 1 coin for daily activity. Now you have 1 coin

23. Activity Lifecycle. In what states can the Activity be?

Lecture



In this lesson:

- Activity LifeCycle - Activity behavior when creating, calling, closing

Theory

When the application is running, we create a new Activity and close the old ones, minimize the application, open it again , etc. Activity can handle all these movements. This is necessary, for example, to free up resources or save data. In the help it is described in sufficient detail.

The Activity created during the operation of the application can be in one of three states :

Resumed - Activity is visible on the screen, it is in focus, the user can interact with it. This state is also sometimes called Running.
Paused - the Activity is not in focus, the user cannot interact with it, but it is visible (it is blocked by another Activity, which does not occupy the entire screen or is translucent).
Stopped - Activity is not visible (completely overlapped by another Activity), respectively, it is not in focus and the user cannot interact with it.

When an Activity goes from one state to another, the system calls its various methods , which we can fill with our own code. Schematically, this can be represented as:

  23. Activity Lifecycle.  In what states can the Activity be?

To simplify understanding, I gave a brief description of the states in brackets under the names. A cross marked the absence of Activity.

So, we have the following Activity methods that the system calls:

onCreate () - called when an Activity is first created
onStart () - called before the Activity will be visible to the user
onResume () - called before it is available for user activity (interaction)

onPause () - called before another Activity is shown
onStop () - called when the Activity is no longer visible to the user
onDestroy () - called before the Activity is destroyed

Those. these methods do NOT cause a state change. On the contrary, changing the state of an Activity is the trigger that calls these methods. Thereby we are notified of the change, and we can respond accordingly. Let's look at the practice, when and in what order these methods are called.

Practice

In this lesson we will need to emulate an event of changing the orientation of the screen. But the emulator with Android 2.3 makes it crooked, so in the project we will use version 2.2. To do this, create a new AVD version 2.2

  23. Activity Lifecycle.  In what states can the Activity be?

Create a project (note, use Android 2.2.):

Project name : P0231_OneActivityState
Build Target : Android 2.2
Application name : OneActivityState
Package name : ru.startandroid.develop.p0231oneactivitystate
Create Activity : MainActivity

Layout does not change, it is not important to us now. We open MainActivity.java , there as usual the default code is:

package ru.startandroid.develop.p0231oneactivitystate;

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

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

We see that the onCreate method, already familiar to us, is implemented. Again, it is important to understand that this method does NOT create an Activity. Creation is a matter of system. Those. the system itself creates an Activity, and gives us the opportunity to participate a little and execute our code in the onCreate () method. We use this opportunity and tell the system that the Activity should display a screen from R.layout.main.

Add all the other methods from the schema, and add an entry to each log .

package ru.startandroid.develop.p0231oneactivitystate;

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

public class MainActivity extends Activity {

final String TAG = "States";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "MainActivity: onCreate()");
}

@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()");
}
}

In kamentah prompted an important note! When implementing these methods, be sure to call the appropriate methods of the super class and always before your code. See code above. Each method contains a call to the super class method and its code is located after these calls.

Now that the methods will be called, we will see it in the logs. Set up a filter on the “States” tag in order not to search for your messages in the general heap of logs. How we do it, we went through lesson 12.

  23. Activity Lifecycle.  In what states can the Activity be?

All save and run the application. After it starts, we look at the log:

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

The activity was created, two states passed (Stopped, Paused) and now it is in the third state - Resumed. Those. it was created (onCreate), displayed (onStart) and got the opportunity to interact with the user (onResume).

Now press the Back button on the emulator. Activity closed. We look at the log:

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

An activity does the opposite of creation. First, it loses focus (onPause), then disappears from the screen (onStop), then it is completely destroyed (onDestroy).

Screen orientation change

Let's see how the Activity behaves when the screen orientation changes. Run the application again (either find it in the list of applications in the system on the emulator, or press CTRL + F11 again in Eclipse). The logs again displayed three methods called upon creation. Now in the emulator, press CTRL + F12, the orientation has changed. It seems that nothing special happened, but we look at the logs and see:

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

Activity is completely destroyed and re-created. In this case, the procedures of saving and restoring data are usually carried out in order not to lose data, and the application retains its appearance. About how this is done, we will talk in the subsequent lessons.

There is also an onRestart method. It is called before the onStart method, if the Activity is not created from scratch, but is restored from the Stoped state. We will cover it in the next lesson.

Usually in textbooks this topic is given differently. But for me this stereotyped explanation seems insufficiently clear, therefore I wrote my own. As always, I hope that I managed to solve the topic)

After this lesson I advise you to read the help, the link to which I gave at the very beginning of the lesson. Everything is very well written there. And knowledge is better absorbed. So far, the main thing is to understand at what point, which method is called. And further we will understand how this can be used and what is there to code.

In the next lesson:

- we study state change on the example of two Activities


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)