Activity各生命周期函数
1 package com.screen;
2
3 import android.app.Activity;
4 import android.content.res.Configuration;
5 import android.os.Bundle;
6 import android.util.Log;
7
8 public class MainActivity extends Activity {
9
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.main);
14 Log.d("isRun", "onCreate");
15 }
16
17 @Override
18 protected void onStart() {
19 super.onStart();
20 Log.d("isRun", "onStart");
21 }
22
23 @Override
24 protected void onResume() {
25 super.onResume();
26 Log.d("isRun", "onResume");
27 }
28
29 @Override
30 protected void onPause() {
31 super.onPause();
32 Log.d("isRun", "onPause");
33 }
34
35 @Override
36 protected void onStop() {
37 super.onStop();
38 Log.d("isRun", "onStop");
39 }
40
41 @Override
42 protected void onDestroy() {
43 super.onDestroy();
44 Log.d("isRun", "onDestroy");
45 }
46
47 @Override
48 protected void onSaveInstanceState(Bundle outState) {
49 super.onSaveInstanceState(outState);
50 Log.d("isRun", "onSave");
51 }
52
53 @Override
54 protected void onRestoreInstanceState(Bundle savedInstanceState) {
55 super.onRestoreInstanceState(savedInstanceState);
56 Log.d("isRun", "onRestore");
57 }
58
59 // 需在AndroidManifest里注册Activity的configChanges属性
60 @Override
61 public void onConfigurationChanged(Configuration newConfig) {
62 super.onConfigurationChanged(newConfig);
63 Log.d("isRun", "onConfigurationChanged");
64 if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
65 Log.d("isRun", "当前屏幕切换成横屏显示");
66 } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
67 Log.d("isRun", "当前屏幕切换成竖屏显示");
68 }
69 }
70
71 }
一.无设置configChanges属性
(1)启动程序
(2)竖屏切换成横屏
(3)横屏切换成竖屏
当我们从横屏切换成竖屏的时候,调用了两次生命周期函数,我对这点感到疑惑,为什么要调用两次呢?
二.在AndroidManifest里添加configChanges的orientation属性
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.screen" android:versionCode="1" android:versionName="1.0">
4 <application android:icon="@drawable/icon" android:label="@string/app_name">
5 <activity android:name=".MainActivity" android:label="@string/app_name"
6 android:configChanges="orientation">
7 <intent-filter>
8 <action android:name="android.intent.action.MAIN" />
9 <category android:name="android.intent.category.LAUNCHER" />
10 </intent-filter>
11 </activity>
12 </application>
13 </manifest>
(1)启动程序
同 一.(1)
(2)竖屏切换成横屏
(3)横屏切换成竖屏
可见,当我们在Manifest文件里设置了android:configChanges="orientation"之后,当我们从横屏切换成竖屏的时候,没有像第一次那样调用了两次生命周期函数,取而代之的而是onConfigurationChanged这个函数,但是,我们从竖屏切换成横屏的时候却没有调用这个函数,这里,我纠结了...为什么呢?
三.在第二步的基础上,在configChanges添加多一个keyboardHidden属性
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.screen" android:versionCode="1" android:versionName="1.0">
4 <application android:icon="@drawable/icon" android:label="@string/app_name">
5 <activity android:name=".MainActivity" android:label="@string/app_name"
6 android:configChanges="orientation|keyboardHidden">
7 <intent-filter>
8 <action android:name="android.intent.action.MAIN" />
9 <category android:name="android.intent.category.LAUNCHER" />
10 </intent-filter>
11 </activity>
12 </application>
13 </manifest>
(1)启动程序
同 一.(1)
(2)竖屏切换成横屏
(3)横屏切换成竖屏
这下又让我纠结了,竖屏切换成横屏,它调用了onConfigurationChanged方法,并且没有执行生命周期函数.横屏切换成竖屏的时候,它也没有执行生命周期函数,但是..它先执行了切换成横屏,然后才切换成竖屏..为什么呢?太纠结...欢迎有所了解的同学来讨论!!!!!