Four knowledge points of activity status that Android beginners must master (required)

These days, I've been trying to drum up the knowledge points of Android. The teachers of interest classes give too profound lectures. Days (I see what I think, and I don't want to hear it later). Finally, I found information and summarized a very important component activity in Android learning. Let's start!

First: master the four states of activity and when to trigger it

First of all, we need to know what an activity is. In short, an activity is actually a screen display page. (simple description)

We know that an activity is managed by the activity stack. When a new activity comes, it will be added to the top of the activity stack, and the previous activity is at the bottom of the activity.

Acitivity generally has four states:

1. Running status: after a new activity is launched into the stack, it is at the front of the screen and at the top of the stack. At this time, it is in a visible and interactive activation state. Android tries to keep it active as much as possible and kill other activities to ensure that the current activity has enough resources to use. When another activity is activated, it will be suspended.

2. Paused state: when the activity is in this state, it is still connected to the window manager. The system continues to maintain its internal state. It is still visible, but it has lost its focus, so it cannot interact with users.

3. Stopped status: when the activity is invisible, the activity is in stopped status. When the activity is in this state, you must save the current data and UI state. Otherwise, once the activity exits or closes, the current data and UI state will be lost

4. Killed status: the activity is in the killed status after it is killed or before it is started. This is because the activity has been removed from the activity stack and needs to be restarted before it can be displayed and used.

A summary of the four states: running state and paused state are visible, while stopped state and killed state are not. Of course, starting is automatically created by the system itself, and each activity is in a certain state. For developers, it is impossible to control the application in a certain state, which is completed by the system.

However, when the state of an activity changes, the developer can get the relevant notification information by calling onxx ().

Next, let's talk about when various states are triggered:

1. Running status:

The functions and sequence triggered during are oncreate() - > on start() - > onresume().

be careful:

1.1: oncreate() is called only when the activity is created for the first time. It is mainly responsible for the general initialization settings of the activity, including view creation, data binding, etc.

It should be noted that if there is a frozen state before (that is, the system has called onsaveinstancestate() function on the activity), the state can be restored through its bundle parameter.

1.2: on start() is called only when the activity is becoming visible. Generally, a broadcast can be registered during this period, and so on.

1.3: onresume() is called when the activity is about to interact with the user. At this time, the activity is at the top of the activity stack.

2. Paused status:

The functions triggered from running state to suspended state and their sequence are: onresume() - > onpuased()

The functions and sequence triggered by resuming the suspended state to the running state are: onpuased() - > onresume().     

be careful:

2.1: onpuased() is called when an activity loses the system focus, including partially obscuring the meeting and turning the device into sleep. Generally, during this period, some unsaved data is persisted and other CPU consuming operations are stopped. At the same time, time-consuming operations are not allowed, otherwise the system UI thread will be blocked.

2.2: an activity in a suspended state will be forced to end by the system only when the system is extremely short of memory resources.

3. Stopped status:

The functions and sequence triggered from pause state to stop state are: onpuased() - > on stop().

The functions and sequence triggered when the stop state returns to the running state are: on stop() - > onrestart() - > on start() - > onresume().

be careful:

3.1: on stop() is called when an activity becomes invisible. At this time, it may be because the activity is to be logged off or the new activity completely blocks the activity. During this period, operations such as canceling registration broadcasting can generally be carried out, so the user is not visible.

3.2: onrestart() is called preferentially when an activity recovers from the stopped state to the running state.

4. Killed status:

There are two situations from stop status to death status: (1) if it is caused by user operation, execute: on stop() - > ondestroy().

(2) if it is automatically enforced by the system, the activity is forcibly terminated.

be careful:

4.1: ondestroy() is called when an activity is being finished by the system.

Second: master the life cycle of activity and the event names and times triggered in each stage

Activity has seven lifecycle functions, namely oncreate(), onrestart(), on start(), onreuse(), onpause(), on stop(), ondestory().

1:onCreate()

Triggered when acitivity is created for the first time. Generally, the things to be done here include creating a view (setcontentview ()), filling the view with necessary data, and so on.

2:onRestart()

If the activity has been stopped before, this method will be triggered before the next OnStart () method. This method will be triggered when the stopped activity needs to be presented to the user again.

3:onStart()

This method will be triggered as long as the activity becomes visible from invisible, but the case that it is obscured / displayed by alertdialog is not included.

4:onResume()

This method is triggered when the activity reaches the top level, that is, when it starts to interact directly with the user. For example, the activity was originally obscured by an alertdialog. When the alertdialog disappears, the onresume () method is triggered.

5:onPause()

On the contrary to the trigger condition of onresume(), if the activity is at the top level, this method will be triggered when it wants to give way to the top level.

Onpause () and onresume () are the two methods that are triggered most frequently, so you should not execute methods that consume too much resources here.

6: onStop ()

This method is triggered when an activity no longer needs to be displayed to the user. If the memory is tight, the system will end the activity directly without triggering the onstop method.

Therefore, saving state information should be done when onpause, not when onstop. If the activities are not running in the foreground, they will be stopped or the Linux management process will end these activities at any time in order to reserve enough storage space for new activities. Therefore, developers must always keep this principle in mind when designing applications. In some cases, the onpause method may be the last method triggered by the activity, so the developer needs to save the information to be saved at this time.

7: onDestroy ()

This method is triggered when the activity is destroyed. Like the onstop method, if the memory is tight, the system will directly end the activity without triggering the method.

Next, we use code to implement specific method triggering:

Create an Android project and edit it in mainactivity

When we start the deployment program, we view the information through the logcat log

The above is about mastering the life cycle of activity and the event names and times triggered in each stage

Third: master how to transfer values between various forms in activity

As mentioned above, we know that activity is a page display of a form. Since it is a page display, we need to display and read data. The problem is how to transfer values between various forms in activity.

A keyword is introduced here: intent: in Android, the intent object is responsible for switching between various activity windows. At the same time, it is also responsible for data transmission.

Next, we use code to implement the analysis (using the eclipse tool):

First: first, we create an Android project named activityvalue and create a common Java class named secondactivity and second in the SRC directory_ Oneactivity Java ordinary classes inherit activity respectively

Create secondactivity and second in directory layout_ Layout XML file corresponding to oneactivity

Named: activity_ second_ one.xml

First, we deploy different types of data m in the secondactivity file. It can be seen from the above that we transfer values through intent

On second_ To edit oneactivity:

If you declare an error in a layout file on line 21, it means that there is no automatic handle generation in the R file. We can do this

Right click on the project to reload the project:

In activity_ A button button is defined in the mainxml layout file

The button binds a method corresponding to the setvaluesmydemo method in mainactivity.java

In the setValuesMyDemo method, the next Activity is called the layout file corresponding to SecondActivity.java.

Then we need to create activity in the layout file corresponding to secondactivity.java_ Add a button to second.xml

activity_ The click event binding method in second.xml is the corresponding mytestone method in secondactivity.java file:

Then witness the miracle moment: we run our program and open our simulator or real machine deployment file

@H_ 301_ 286@

Third: master the problem of saving the state of the activity form

How to save the current form state here is realized through code:

Based on the above case, we rewrite the two methods of activity in the mainactivity.java file:

onRestoreInstanceState(Bundle savedInstanceState)

onSaveInstanceState(Bundle outState)

After that, we started to run the program, and the final results are as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>