1. FirstActivity:

 1 package com.example.activitycircledemo;
 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 import androidx.annotation.Nullable;
10 
11 public class FirstActivity extends Activity {
12     private static final String TAG = "FirstActivity";
13 
14     @Override
15     protected void onCreate(@Nullable Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_first);
18         Log.d(TAG, "onCreate...");
19     }
20 
21     /**
22      * onStart已经可见了,但是没有焦点,也就是没有获取到焦点,不可以进行操作
23      */
24     @Override
25     protected void onStart() {
26         super.onStart();
27         Log.d(TAG, "onStart...");
28     }
29 
30     /**
31      * 可见,并且已经获取到焦点,也就是可以操作,可以进行交互
32      */
33     @Override
34     protected void onResume() {
35         super.onResume();
36         Log.d(TAG, "onResume...");
37     }
38 
39     /**
40      * onPause其实是暂停的意思,这个方法其实是失去了焦点,不可以操作,但是可以可见
41      */
42     @Override
43     protected void onPause() {
44         super.onPause();
45         Log.d(TAG, "onPause...");
46     }
47 
48     /**
49      * onStop和onDestroy都是失去了焦点并且不可见
50      */
51     @Override
52     protected void onStop() {
53         super.onStop();
54         Log.d(TAG, "onStop...");
55     }
56 
57     @Override
58     protected void onDestroy() {
59         super.onDestroy();
60         Log.d(TAG, "onDestory...");
61     }
62 
63     public void skipToSecondActivity(View view){
64         Intent intent = new Intent(this, SecondActivity.class);
65         startActivity(intent);
66 
67     }
68 }

 

2. SecondActivity:

 1 package com.example.activitycircledemo;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 
 6 import androidx.annotation.Nullable;
 7 
 8 public class SecondActivity extends Activity {
 9     @Override
10     protected void onCreate(@Nullable Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_second);
13     }
14 }

 

3. activity_first

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical">
 7 
 8     <TextView
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_centerInParent="true"
12         android:text="我是第一个界面"
13         android:textSize="25sp"/>
14 
15     <Button
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:text="跳转到第二个界面"
19         android:onClick="skipToSecondActivity"
20         android:layout_centerHorizontal="true"/>
21 
22 </RelativeLayout>

 

4. activity_second:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical">
 7 
 8     <TextView
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_alignParentBottom="true"
12         android:textSize="25sp"
13         android:layout_centerHorizontal="true"
14         android:text="我是第二个界面"/>
15 
16 
17 </RelativeLayout>

 

5. Manifest注册:

 1  <activity android:name=".FirstActivity">
 2             <intent-filter>
 3                 <action android:name="android.intent.action.MAIN" />
 4 
 5                 <category android:name="android.intent.category.LAUNCHER" />
 6             </intent-filter>
 7         </activity>
 8 
 9         <activity android:name=".SecondActivity"
10             android:theme="@android:style/Theme.Translucent">
11 <!--            设置改activity为透明的-->
12         </activity>

  这里将第二个界面设置成透明的,是为了更好的理解onPause和onStop的作用范围。

 

6. 运行结果

  首先第一次启动时,调用onCreate,onStart,onResume,此时有焦点并且可见。

 

  logcat:

      com.example.activitycircledemo D/FirstActivity: onCreate...

      com.example.activitycircledemo D/FirstActivity: onStart...

      com.example.activitycircledemo D/FirstActivity: onResume...

   当点击按钮后,跳转到第二个界面,由于第二个界面透明,这使得第一个界面可见,但依旧失去了焦点,此时仅调用onPause。

 

   logcat:

       com.example.activitycircledemo D/FirstActivity: onPause...

  此时如果退出第二个Activity,那么便会调用onResume。

  本质上函数都是成双成对调用的。例如onStart和onStop,onResume和onPause。

posted on 2021-09-30 17:08  EndlessShw  阅读(226)  评论(0编辑  收藏  举报