Detailed explanation of complete life cycle examples of activity in Android programming

This paper analyzes the complete life cycle of activity in Android programming. Share with you for your reference, as follows:

Activity in Android has its own life cycle. Learning these knowledge can help us better understand some errors when writing programs in the future. This article is very long. I hope it won't waste everyone's time ~

Today, I won't talk much about the activity stack, mainly about the life cycle of the activity itself

Distinguish several concepts

1 an activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface, An operation currently in progress by the user.

2 back stack the user will start an activity a through the application launcher by touching the program, and the started activity a will be pushed into the top of the stack. If the current activity a starts a new activity B, then a calls onstop function, and the system will maintain the activity information. When the user presses the back key, the back stack will perform a pop operation, And call onresume() of A. the specific details of entering and leaving the stack will be described in detail later.

3 tasks: when the user is in an activity, the name of activity a is "When the taskone application is started, press the home key and the user returns to the launcher. At this time, if the user touches the new application again, a new task will be created, and a back stack represents a task. The activities of different programs can be pressed into the same stack, that is, they can form a task. For example, your program starts a system's own SMS activity, The feeling to users is that texting seems to be a function of your program.

Note: the above behaviors are the default settings of the system. There are two ways to change the behavior of the activity. Add a to start B. one is to change the behavior in the manifest setting of B, and the other is to specify the activity setting to be started in the intent initiated by the activity. The priority of the intent is higher than that of the manifest.xml file, And some modes do not exist in both ways at the same time.

The life cycle of Android includes oncreate OnStart onrestart onpause onstop ondestroy. In the declaration cycle, the activity will call the above methods to perform the operations corresponding to different states. The call time of each method is described below

This is where you should do all of your normal static set up: create views, bind data to lists, etc. this method also provides you with a bundle containing the activity's previously frozen state, if there was one

Always followed by onstart(). / / called when the activity is created for the first time. You should do all static creation in this method, create views, bind data to lists (through setcontnetview method), and so on. If there is a saved state, this method also provides you with a bundle containing the latest state of the activity. The onStart method is always called after this method.

Activity can not be terminated in onRestart () times state Called after your activity has been stopped, prior to, stopped, your, and / or called after your stop, before calling back.

Onresume() is called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it. Always followed by onpause(). / / called when the activity will start interacting with the user. At the moment, your activity is at the top of the activity stack for user input. It will always be called / / after onpause

Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns. Followed by either onResume() if the activity returns back to the front,or onStop() if it becomes invisible to the user.

//It is called when the system is about to restart the previous activity (I don't understand, my own understanding is that it is called when the current activity wants to start a new activity). Typical applications are used to submit unsaved data to the current data (that is, save data updates), stop animations and other operations that may consume CPU. The implementation of this method must be fast because the next activity will not be restarted until this method returns.

When the activity changes from back to front, the onresume method will be called after the onpause method

When the activity becomes invisible, the onstop method is called after onpause

Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being broken in front of this one, or this one is being destroyed. Followed by either onrestart() if this activity is coming back to interact with the user,or onDestroy() if this activity is going away.

//Called when the activity is no longer visible to the user because another activity has restarted and overwrites the current activity (on the stack). When a new activity is started, or an existing activity returns to the foreground state, or the current activity will be destroyed. If activity wants to go back to the front desk to interact with the user, then the onReatart method is called after this method. If the current activity is to disappear, the onDestroy method will be called after this method.

The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish()) on it,or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

This is the last method received when your activity dies. There are two situations for calling this method: one is that the activity is about to be completed, which can be realized by calling the finish method. Instead, the system destroys the instance of activity to free up space. You can use the isfinish method to distinguish between the two situations. This method is often used in the onpause method to determine whether the activity is suspended or will terminate. This function will be demonstrated in a later demo.

The following figure is a life cycle demonstration of the official website

Well, let's take a look at an example of a demonstration I wrote,

MainFest.xml

Layout file main.xml

activity01.xml

String.xml

ActivityDemoActivity.java

Activity01.java

Here are the results of the demonstration,

The operation process is: start activitydemoactivity

Then click the button to enter activity01

(it can be seen that the activity is suspended first and will not be released. In fact, it is a stack pressing process for a new activity, and then the new activity starts. It should be oncreate and then OnStart. I printed the statement incorrectly. Careful friends should see that when the old activity is not visible, call its onstop method.)

Press the back key again to return to activitydemoactivity

(after returning, activity01 at the top of the stack will pop the stack, and the display will be destroyed)

Press the back key again to return to the desktop

In fact, dongxie, which is not complex, has written for a long time, but basically shows the complete life cycle of activity.

I hope this article will help you in Android programming.

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
分享
二维码
< <上一篇
下一篇>>