找回密码
 立即注册
ACTIVITYtheonResumeprotected | 软件设计/软件工程 2022-05-02 217 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
什么是生命周期

在没有接触android开发的时候,听到有人说Activity生命周期,我觉得这是一个很专业很深刻的东西。

但是了解了之后,原来不是一回事。这个Activity生命周期其实是一个很基础的东西。了解他们的原则将有助于我们在未来的发展。

编写好的程序有很大帮助。

生命周期其实就是一个事物从诞生到死亡的这段时间,那么Activity生命周期呢?

我们先借一张官方图来说明,看下图。



从图中可以看出,在Activity的生命周期中,它们的行为是由不同阶段的不同函数控制的。当然这种行为可以由用户自己决定。

我们不要看图片左侧的箭头,我们看的是图片的中间和右侧部分。





循环的第一阶段:生活阶段,即从开始到跑步阶段。

特点:在窗口的最前面,用户可以看到整个Activity

函数执行顺序:onCreate()->onStart()->onResume()

为了证明这些函数确实被执行了,我们编写如下代码:

1 package com.aidevelops.activitys;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.util.Log;
6
7 public class MainActivity extends Activity {
8
9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13         Log.d("Active", "onCreate method started");
14     }
15
16     @Override
17     protected void onResume() {
18         super.onResume();
19         Log.d("Active", "onResume method started");
20     }
21
22     @Override
23     protected void onStart() {
24         super.onStart();
25         Log.d("Active", "onStart method started");
26     }
27     
28     
29     
30 }
View Code
Log.d(tag, message)函数用于将指定tag的message打印到logcat控制台

我们可以将tag理解为一个分类

运行程序后打开LogCat窗口,创建一个条件过滤器

如图:

  

点击绿色的+号后弹出一个窗口:

  

注意Log.d()函数的第一个参数就是by log Tag中的Tag,第二个参数是要打印到LogCat控制台的信息

填好后点击OK。接着点刚才创建的LiifeCycle过滤器,就能看到下图的信息:
正如我们所见,每个函数都是按顺序执行的。



第二阶段:暂停阶段

特征:活动被部分遮挡。 比如被半透明窗口遮挡。

涉及的函数:onPause()、onResume()

第三阶段:停止阶段

特点:Activity被完全阻塞,例如打开一个新的Activity。

涉及的函数:onPause()、onStop()

当用户导航回到之前停止的 Activity 时,会触发停止的 Activity 重新启动。

涉及函数:onRestart()->onStart()->onResume()

如图所示:

当我们启动另一个 Activity 时会触发 onPause 和 onStop

而onRestart、onStart、onResume都是在我们点击返回按钮的时候触发的



如果有兴趣,可以自己编写代码进行测试。 和上面的代码一样,重写对应的声明循环函数,填入你要测试的代码。

粘贴完整代码:
1 package com.aidevelops.activitys;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.util.Log;
7 import android.view.View;
8
9 public class MainActivity extends Activity {
10
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_main);
15         Log.d("Active", "onCreate method started");
16     }
17
18     @Override
19     protected void onResume() {
20         super.onResume();
21         Log.d("Active", "onResume method started");
22     }
23
24     @Override
25     protected void onStart() {
26         super.onStart();
27         Log.d("Active", "onStart method started");
28     }
29
30     @Override
31     protected void onPause() {
32         super.onPause();
33         Log.d("Active", "onPause method started");
34     }
35
36     @Override
37     protected void onRestart() {
38         super.onRestart();
39         Log.d("Active", "onRestart method started");
40     }
41
42     @Override
43     protected void onStop() {
44         super.onStop();
45         Log.d("Active", "onStop method started");
46     }
47
48     public void onClickStartActivity(View view)
49     {
50         Intent intent = new Intent(this, TargetActivity.class);
51         startActivity(intent);
52     }
53     
54 }

(What is the life cycle
When I was not in touch with Android development, I heard someone say about the activity life cycle. I think it is a very professional and profound thing.
But after understanding, it's not the same thing. This activity life cycle is actually a very basic thing. Understanding their principles will help us develop in the future.
Writing a good program is very helpful.
The life cycle is actually the period from birth to death of a thing. What about the activity life cycle?
Let's borrow an official picture to illustrate. See the figure below.
As can be seen from the figure, in the life cycle of activities, their behavior is controlled by different functions in different stages. Of course, this behavior can be determined by the user.
Instead of looking at the arrows on the left side of the picture, we look at the middle and right parts of the picture.
The first stage of the cycle: the life stage, that is, from the beginning to the running stage.
Features: in the front of the window, users can see the whole activity
Function execution order: oncreate() - > onstart() - > onresume()
To prove that these functions are actually executed, we write the following code:
1 package com. aidevelops. activitys;
two
3 import android. app. Activity;
4 import android. os. Bundle;
5 import android. util. Log;
six
7 public class MainActivity extends Activity {
eight
9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super. onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13         Log. d("Active", "onCreate method started");
14     }
fifteen
16     @Override
17     protected void onResume() {
18         super. onResume();
19         Log. d("Active", "onResume method started");
20     }
twenty-one
22     @Override
23     protected void onStart() {
24         super. onStart();
25         Log. d("Active", "onStart method started");
26     }
twenty-seven
twenty-eight
twenty-nine
30 }
View Code
Log. The D (tag, message) function is used to print the message of the specified tag to the logcat console
We can understand tag as a category
After running the program, open the logcat window and create a conditional filter
As shown in the figure:
Click the green + sign to pop up a window:
Note log The first parameter of d() function is tag in by log tag, and the second parameter is the information to be printed to logcat console
Click OK after filling in. Then click the liifecycle filter just created, and you can see the following information:
As we can see, each function is executed sequentially.
Phase II: suspension phase
Feature: the activity is partially obscured. Such as being obscured by a translucent window.
Functions involved: onpause(), onresume()
Phase III: stop phase
Features: the activity is completely blocked, such as opening a new activity.
Functions involved: onpause(), onstop()
When the user navigates back to the previously stopped activity, it will trigger the restart of the stopped activity.
Functions involved: onrestart() - > onstart() - > onresume()
As shown in the figure:
When we start another activity, onpause and onstop will be triggered
Onrestart, OnStart and onresume are triggered when we click the return button
If you are interested, you can write your own code for testing. Like the above code, rewrite the corresponding declaration loop function and fill in the code you want to test.
Paste full code:
1 package com. aidevelops. activitys;
two
3 import android. app. Activity;
4 import android. content. Intent;
5 import android. os. Bundle;
6 import android. util. Log;
7 import android. view. View;
eight
9 public class MainActivity extends Activity {
ten
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super. onCreate(savedInstanceState);
14         setContentView(R.layout.activity_main);
15         Log. d("Active", "onCreate method started");
16     }
seventeen
18     @Override
19     protected void onResume() {
20         super. onResume();
21         Log. d("Active", "onResume method started");
22     }
twenty-three
24     @Override
25     protected void onStart() {
26         super. onStart();
27         Log. d("Active", "onStart method started");
28     }
twenty-nine
30     @Override
31     protected void onPause() {
32         super. onPause();
33         Log. d("Active", "onPause method started");
34     }
thirty-five
36     @Override
37     protected void onRestart() {
38         super. onRestart();
39         Log. d("Active", "onRestart method started");
40     }
forty-one
42     @Override
43     protected void onStop() {
44         super. onStop();
45         Log. d("Active", "onStop method started");
46     }
forty-seven
48     public void onClickStartActivity(View view)
49     {
50         Intent intent = new Intent(this, TargetActivity.class);
51         startActivity(intent);
52     }
fifty-three
54 }
)





上一篇:IOS零碎技术整理(三)——获取WIFI列表
下一篇:DEBUG_NEW和THIS_FILE