The first Activity of the four main components of the Android back-to-furnace series, and the android back-to-furnace
I haven't really studied android for a while. After all, android is the first technology I have come into contact with in this software development circle. android has become a mantra. when I first learned android, it was mostly a bit tricky, and many knowledge points were not thoroughly studied. As a result, the current foundation is not very reliable. in the face of some highly technical problems, the idea is easy to break, so I decided to use the blog to record the process of back-to-furnace learning. I want to write something that belongs to myself, but I want to write something that is too cool but easy to understand. There won't be a lot of code for beginners to understand, I will comment out the meaning of each line of the Code that appears. I have always considered myself a newbie. the swordsmanship has a line saying, "a real master always has an apprenticeship heart ". haha, although I am not a master.
Activitty is the first among the four android components. It is almost the most important component. Let's go back to the furnace from Activity today.
1. What is Activity?
The four main components of Android are Activty/Service/Broadcast Explorer/Content Provider.Activity translation means Activity. In android, it can be understood as an interface that the user can see and interact with the user. it can be said that all the things you can see in an app are Activity. we can put a lot of things on an Activity, such as lists, images, buttons, and so on. then, other components are used to make these "dead" things "active. how to make them active will be updated in subsequent blogs.
1.1 lifecycle of an Activity
To learn an Activity, you have to mention its lifecycle. It is crucial! Every part of the life cycle plays a unique and irreplaceable role in the life of the Activity, just as we have many crucial periods in our life.
To understand the life cycle, you must have a deep understanding of the running status of the Activity, which roughly goes through four running statuses.
A. Running status: the Activity is in the foreground, that is, you can see it, and you can get the focus. (The focus is the meaning that you can operate and interact)
B. paused: The Activity is still in the foreground and visible, but the focus is lost. for a common scenario, you will understand it. for example, if you are playing, a text message is sent. The dialog box of this text message is not full of the screen, but a small part. At this time, you can reply to the text message, some interfaces can be seen but cannot be operated. the current Activity is paused.
C. stop status: Activity is invisible and the focus is lost. for example, if you exit from a person's dialog box page to see the circle of friends, then the Activity in the dialog box is stopped. you can't see him anymore.
D. Destroy state: Activty is completely over. It is like a person's life is over, but life cannot be restarted, but Activity can be restarted ~
After learning about the running status of an activity, you should understand how the specific life cycle runs. Unknown life cycles are hidden behind every running status. Haha ~
Activity has a total of seven lifecycles: onCreat () onStrat () onResume () onPause () onStop () onDestory () onRestart (). you can understand their meanings in English. if you are careful, you can find that they correspond to the beginning and end.
// OnCreat is the first method in the lifecycle, which means creating! In other words, this method is called when an Activity is created. Some initialization operations can be performed in this method, such as loading layout and initialization methods.
@ Override protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );}
// OnStart is the method called when Activty is created and displayed on the screen. In other words, you can see the Activity but cannot interact with it.
@ Override protected void onStart (){
Super. onStart ();}
// When onResume is called back, it can interact with the Activity. For example, you can click some buttons or images.
@ Override protected void onResume (){
Super. onResume ();}
// The onPause method is called when the pause state is mentioned above, for example, if you are sending a text message and suddenly make a call, you need to call this method to save the page you are sending a text message. You can write some methods in it to save the content you are inputting, otherwise, no text message is sent when your phone call goes down. Well, no one wants this kind of user experience? This is the role of this method!
@ Override protected void onPause (){
Super. onPause ();}
// The onStop method is to stop calling an Activity immediately after onPause. To stop the Activity, you must first pause it. For example, if you press the home Key instead of the Return key, the Activity is minimized and not destroyed.
@ Override protected void onStop (){
Super. onStop ();}
// OnDestory: destruction follows onStop. Similarly, an activity must be stopped before destruction.
@ Override protected void onDestroy (){
Super. onDestroy ();}
// The onRestart method is used to restart an Activity. This method is called when the system re-opens the activity after the Activity is destroyed.
@ Override protected void onRestart (){
Super. onRestart ();}
If this is the case, you still do not fully understand the lifecycle process. I can only say that it is normal, because I did not write well! We recommend that you use the log method to print the logs and switch back and forth between the two activities to see how these methods are called.