1. 先写一个程序来查看结果,HVTransform(详情内容见注释):

 1 package com.example.activitycircledemo;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.util.Log;
 6 import android.widget.SeekBar;
 7 
 8 import androidx.annotation.Nullable;
 9 
10 /**
11  * Activity在横竖屏幕切换时,生命周期会发生变化
12  * com.example.activitycircledemo D/HVTransform: onCreate...
13  * com.example.activitycircledemo D/HVTransform: onStart...
14  * com.example.activitycircledemo D/HVTransform: onResume...
15  * ...(切换状态)
16  * com.example.activitycircledemo D/HVTransform: onPause...
17  * com.example.activitycircledemo D/HVTransform: onStop...
18  * com.example.activitycircledemo D/HVTransform: onDestory...
19  * com.example.activitycircledemo D/HVTransform: onCreate...
20  * com.example.activitycircledemo D/HVTransform: onStart...
21  * com.example.activitycircledemo D/HVTransform: onResume...
22  * 由此可知,当切换屏幕状态时,Activity会自动销毁然后重新创建
23  *
24  * 横竖屏幕切换的场景
25  * 1. 游戏开发
26  * 2. 视频播放器
27  * ......
28  * 由于屏幕切换时会导致Activity的销毁重建,然而在实际体验中需要保留状态。
29  * 第一种方式就是禁止旋转,指定屏幕的方向,该方法在游戏中运用较多
30  *      在Manifest中,在activity中添加属性:android:screenOrientation=landscape(水平)或者portrait(垂直)
31  * 第二种方式就是对配置不敏感,适用与视频播放器的开发
32  *      在Manifest中,在activity中添加属性:android:configChanges="keyboardHidden|screenSize|orientation"(一般这三个连用)
33  *      表示当发生键盘的隐藏,屏幕大小和朝向变化时,保留配置不改变生命周期。旋转现在是既改变大小,又改变方向
34  *
35  */
36 public class HVTransform extends Activity {
37     private static final String TAG = "HVTransform";
38     private SeekBar mSBProgress;
39 
40     @Override
41     protected void onCreate(@Nullable Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         setContentView(R.layout.activity_hvtransform);
44         Log.d(TAG, "onCreate...");
45         initView();
46     }
47 
48     private void initView() {
49         mSBProgress = (SeekBar) this.findViewById(R.id.progress);
50         // 通过输出控件的hash值,可以判断控件是否一直是同一个,实际上是不同的
51         Log.d(TAG, "mSBProgress == " + mSBProgress.toString());
52         // 设置进度条长度和初始值
53         mSBProgress.setMax(100);
54         mSBProgress.post(new Runnable() {
55             @Override
56             public void run() {
57                 mSBProgress.setProgress(0);
58             }
59         });
60     }
61 
62     @Override
63     protected void onStart() {
64         super.onStart();
65         Log.d(TAG, "onStart...");
66     }
67 
68     @Override
69     protected void onResume() {
70         super.onResume();
71         Log.d(TAG, "onResume...");
72     }
73 
74     @Override
75     protected void onPause() {
76         super.onPause();
77         Log.d(TAG, "onPause...");
78     }
79 
80     @Override
81     protected void onStop() {
82         super.onStop();
83         Log.d(TAG, "onStop...");
84     }
85 
86     @Override
87     protected void onDestroy() {
88         super.onDestroy();
89         Log.d(TAG, "onDestory...");
90     }
91 }

 

 

2. logcat的结果显示(没有在Manifest中添加Android属性):

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

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

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

  ...(切换状态)

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

  com.example.activitycircledemo D/HVTransform: onStop...

  com.example.activitycircledemo D/HVTransform: onDestory...

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

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

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

 

  

posted on 2021-10-02 17:31  EndlessShw  阅读(56)  评论(0编辑  收藏  举报