本系列文章均为A2BGeek原创,转载务必在明显处注明:
转载自A2BGeek的【Android每周专题】系列,原文链接:http://blog.csdn.net/a2bgeek/article/details/14119801
每周专题名不符实,实在是惭愧,前段时间有点忙,没有及时更新,自我反省一下。
本周专题分为两个部分,第一部分是横竖屏切换的相关问题,第二部分是Activity中数据状态的保持。
1、启动Activity(竖屏时启动的)
11-01 01:48:17.144: I/a2bgeek(945): onCreate
11-01 01:48:17.594: I/a2bgeek(945): onStart
11-01 01:48:17.594: I/a2bgeek(945): onResume
2、由竖屏切换为横屏(把真机横过来或者Ctrl+F11把模拟器横过来)
11-01 01:50:45.533: I/a2bgeek(945): onPause
11-01 01:50:45.533: I/a2bgeek(945): onStop
11-01 01:50:45.543: I/a2bgeek(945): onDestroy
11-01 01:50:45.593: I/a2bgeek(945): onCreate
11-01 01:50:45.934: I/a2bgeek(945): onStart
11-01 01:50:45.934: I/a2bgeek(945): onResume
3、由横屏再切换为竖屏
11-01 01:51:15.214: I/a2bgeek(945): onPause
11-01 01:51:15.214: I/a2bgeek(945): onStop
11-01 01:51:15.214: I/a2bgeek(945): onDestroy
11-01 01:51:15.277: I/a2bgeek(945): onCreate
11-01 01:51:15.704: I/a2bgeek(945): onStart
11-01 01:51:15.704: I/a2bgeek(945): onResume
从日志中可以清楚地看到,横竖屏切换会让Activity销毁再重建。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="input something" /> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <EditText android:id="@+id/et" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="input something" /> </LinearLayout>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
<activity android:name="com.a2bgeek.orientationswitchdemo.MainActivity" android:configChanges="orientation|screenSize" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>3、重写Activity中的onConfigurationChanged方法
@Override public void onConfigurationChanged(Configuration newConfig) { // TODO Auto-generated method stub super.onConfigurationChanged(newConfig); Log.i(TAG, "onConfigurationChanged"); if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { setContentView(R.layout.activity_main); } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { setContentView(R.layout.activity_main); } }其实不用判断条件,直接setContentView就可以,Android系统会自动使用。但是这样View的状态是无法保持的,因为View状态的恢复是在onRestoreInstanceState中执行的,从日志中我们看到Activity根本没有销毁和重建,那么何来onRestoreInstanceState的执行呢?
在这部分将会说明Activity在正常的用户使用逻辑中如何保持良好的用户体验。
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Log.i(TAG, "onSaveInstanceState"); Parcelable savedState = textView1.onSaveInstanceState(); outState.putParcelable(kTextView1Name, savedState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.i(TAG, "onRestoreInstanceState"); SavedState0 savedState = savedInstanceState .getParcelable(kTextView1Name); textView1.onRestoreInstanceState(savedState); }而几乎所有的View都实现了自己的 onSaveInstanceState和onRestoreInstanceState,所以View的状态不太需要开发者来操心。
@Override public Object onRetainNonConfigurationInstance() { final MyDataObject data = collectMyLoadedData(); return data; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance(); if (data == null) { data = loadMyData(); } ... }
写这个系列文章的初衷是总结和归纳,但是在写的过程中发现自己掌握的知识还很肤浅,所以这个系列的每篇文章我都会认真对待,如果有了新的体会,就会更新。
参考资料:
1、http://hubingforever.blog.163.com/blog/static/17104057920106272410547/
2、http://developer.android.com/training/basics/activity-lifecycle/recreating.html
3、http://developer.android.com/guide/topics/resources/runtime-changes.html