Android road ------- explanation of Activity, androidactivity
Preface
Due to the approaching holiday, the company was busy working on projects, so LP was busy some time ago and had no time to summarize and write a blog. It was just preparing to read a book while going to bed, and there was not much reading every day, about 10 pages, but every day, it is a huge number.
Today, the LP task is completed, and a blog about Activity will be published before the leader has assigned the task ..........
In the previous article, I shared with you the development history of Android, the system framework, and the four components of Android. LP will introduce the Activity of one of the four components.
After learning about the history of Android, have you impressed Android with the spirit of not dying? If so, hurry and learn about Android ..
Let's go.
Directory
- Activity)
- Who cares about the activity?
- How does it happen?
- Startup Mode
- How to use it?
- How do brothers interact?
Activity, one of the four Android Components]
Speaking of Activity, I believe that even people without the android Foundation will know what Activity is, because it will often appear around us, but when it comes to Activity, it is not necessarily the case, however, people who know a little English can also understand the meaning of activity. In fact, Android activity is very similar to activity in real life and can also be understood as activity in real life, let's officially start.
Speaking of Android Activity, what is it? It is a component in the Android system and is responsible for interacting with users. Every interface seen by users is an activity.
In Android, Activity has four basic states.
1. running status active/running
It is at the front end of the screen or is interacting with the user. It is called the running or activation status.
2. Paused
The current activity loses focus. When it is covered by a window or a non-full screen, it is called a paused state.
3. Stoped
The current activity is completely invisible. It is called the stop status.
4. The kill state is Killed.
When an activity is in the "paused" Or "STOPPED" status, the system can delete Or terminate the current activity from the memory, in addition, when the system memory is insufficient, the former action is also executed.
Watch movies
If you cannot understand the above, you can understand it as watching a movie. The movie is the current activity.
1. When a movie is being played, it is called a running state.
2. When we click the pause button, isn't the screen dimmed? Or a small advertisement may appear. This can be understood as being covered by another full-screen transparent thing or a non-full-screen small advertisement. This is paused.
3. I have watched many Hong Kong dramas. If I don't want to watch them, I just want to change to a Japanese action movie. Isn't the Hong Kong Drama invisible now? Or the computer has a black screen. You can't see the Hong Kong opera, that is, the current activity. These can be called the stop status.
4. If you don't want to watch a movie, manually turn off the player. The player is paused or stopped. When the memory is insufficient, the system will automatically turn off the player.
You don't need to drill down the corners of the artifact. You can see it later .........
Who should take care of the Activity?
Activities are managed by stacks. They are first-in-first-out and later-in-first-out. The activity that every user can see is at the top of the stack. When the memory is insufficient, the bottom activity in the stack will be recycled, Which is straightforward and clear.
How is it born and dead? (Lifecycle)
I will not talk about this. Just start ..
On the official lifecycle chart, LP touched the following points:
The figure is finished, followed by the Code:
1 package com. leo. writeblog; 2 3 import android. app. activity; 4 import android. OS. bundle; 5 import android. util. log; 6 7 public class MainActivity extends Activity {8 private final String TAG = "MainActivity"; 9 @ Override10 protected void onCreate (Bundle savedInstanceState) {11 super. onCreate (savedInstanceState); 12 setContentView (R. layout. activity_main); 13} 14 @ Override15 protected void onStart () {16 super. onStart (); 17 Log. I (TAG, "onStart"); 18} 19 @ Override20 protected void onResume () {21 super. onResume (); 22 Log. I (TAG, "onResume"); 23} 24 @ Override25 protected void onRestart () {26 super. onRestart (); 27 Log. I (TAG, "onRestart"); 28} 29 @ Override30 protected void onPause () {31 super. onPause (); 32 Log. I (TAG, "onPause"); 33} 34 @ Override35 protected void onStop () {36 super. onStop (); 37 Log. I (TAG, "onStop"); 38} 39 @ Override40 protected void onDestroy () {41 super. onDestroy (); 42 Log. I (TAG, "onDestroy"); 43} 44}View Code
The first running log is as follows:
When you press the return key to exit, the log is as follows:
The log is as follows:
The logs returned from the application are as follows:
In addition, when the activity is visible, that is, when a small window is displayed or a transparent activity is displayed, only the onPause method is executed. When the activity gets the focus again, the onResume method is executed.
OnCreate: The activity is called only once when it is run for the first time.
OnRestart: called when activity is invisible and visible again
OnStart: called when activity is visible
OnStop: called when activity is invisible
OnResume: called when activity gets focus
OnPause: called when activitiy loses focus
OnDestroy: called when activity is destroyed
Startup Mode
When it comes to the startup mode, there are four types: standard, singleTop, singleTask, and singleInstance, which can be found in AndroidManifest. in the xml file, use the android: launchMode attribute in the <activity> label to select the startup mode.
Standard:
This mode is a standard start mode and the default start mode. This mode is automatically used for all activities without explicitly specifying it.
SingleTop
When the startup mode is singleTop, if you find that the site of the returned stack is already in this activity when you start the activity, you can directly use it without having to create
SingleTask
When the startup mode is singleTask, each startup activity will check whether the activity exists in the returned stack. If so, all the activities above the concurrent activity will go out of the stack; otherwise,
SingleInstance
The startup mode is singleInstance. A return stack is created separately to manage the activity, so that the activity can be shared between the program and the program.
How to use it?
When creating an activity, we must inherit the Activity that comes with Android. When using the activity, we must register it in the AndroidManifest. xml file, as shown below:
How do they communicate with each other?
It refers to communication, data transmission, and activity activation through Intent (Intent ).
What is intent?
In fact, it is like a courier in our life, responsible for helping us deliver things. The intention in the Android system is to assume this role...
It can communicate with activities of different programs, or start activities that are not in the same program ...... wait a moment. I will not discuss more here. If you need to learn more, you can check the information on the Internet.