Reference: http://blog.csdn.net/aliaooooo/article/details/23606179
1. Handling Runtime Changes
Some Device Configurations can change during runtime (such as-screen orientation, keyboard availability, and language). when such a
Change occurs, Android restarts the running Activity( onDestroy()
was called, followed by onCreate()
). The restart behavior is designed to
Help your application adapt to new configurations by automatically reloading your application with alternative resources T Hat match the
New device configuration.
To properly handle a restart, it's important that your activity restores its previous state through the normal a Ctivity lifecycle, in which
Android calls onsaveinstancestate () before it destroys your activity so, can save data about the Applicat Ion state. You can then
Restore the state during onCreate()
or onRestoreInstanceState()
.
However, might encounter a situation in which restarting your application and restoring significant amounts of data CA N Be costly and
Create a poor user experience. In such a situation, you have the other options:
<1>retaining an Object During a Configuration change
<2>handle The configuration Change Yourself
2. Retaining an Object During a Configuration change
If Restarting your activity requires that you recover large sets of data, re-establish a network connection, or perform OT Her intensive
Operations, then a full restart due to a configuration change might is a slow user experience.
Also, it might not being possible for your completely restore your activity Bundle
state with the the that the system s Aves
onSaveInstanceState()
Callback-it is isn't designed to carry large objects (such as bitmaps) and the data within it must be Seriali Zed Then
deserialized, which can consume a lot of memory and make the configuration change slow.
In such a situation, you can alleviate the burden of reinitializing your activity by retaining a Fragment
when your Activity is restarted due to a
Configuration change. This fragment can contain references to stateful objects so you want to retain.
When the Android system shuts down your activity due to a configuration change, the fragments of your activity so you ha ve marked to
Retain is not destroyed. You can add such fragments to your activity to preserve stateful objects.
To retain stateful objects in a fragment during a runtime configuration change:
<1> Extend the Fragment class and declare refrences to your stateful objects
<2> Call Setretaininstance (Boolean) when the fragment is created
<3> ADD The fragment to your activity
<4> use Fragmentmanager to retrieve the fragment then the activity is restarted
//Configurationchangehandlertest.xml<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"Tools:context="mirror.android.configurationchangehandlertest.ConfigurationChangeHandlerTest"> <TextView android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Click the button to start the thread simulation network time-consuming operation, get news details displayed under the button"/> <Button Android:id="@+id/btn_createthread"Android:layout_width="match_parent"Android:layout_height="wrap_content"Android:text="Get Entertainment News"/> <TextView Android:id="@+id/tv_shownews"Android:layout_width="match_parent"Android:layout_height="match_parent"Android:textcolor="@android: Color/holo_green_dark"/></linearlayout>
//Configurationchangehandlertest.javaPackage Mirror.android.configurationchangehandlertest;import Android.app.activity;import Android.app.progressdialog;import Android.content.res.configuration;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.util.log;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.progressbar;import Android.widget.TextView; Public classConfigurationchangehandlertest extends Activity implements Onclicklistener {PrivateFinal String TAG ="configuartiontest"; PrivateString Newsinfo; Privatebutton button; PrivateTextView Shownewstextview; PrivateProgressDialog ProgressDialog; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_configuration_change_handler_test); Button=(Button) Findviewbyid (R.id.btn_createthread); Button.setonclicklistener ( This); Shownewstextview=(TextView) Findviewbyid (r.id.tv_shownews); if(Getresources (). GetConfiguration (). Orientation = =Configuration.orientation_landscape) {log.i (TAG,"----Oncreate-landscape---"); } Else if(Getresources (). GetConfiguration (). Orientation = =configuration.orientation_portrait) {log.i (TAG,"----oncreate-portrait---"); }} @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.btn_createthread:excutelongtimeoperation (); Break; } } Private voidexcutelongtimeoperation () {ProgressDialog= Progressdialog.show (configurationchangehandlertest. This , "Load Info" , "Loading" , true , true); Thread Workerthread=NewThread (NewMynewthread ()); Workerthread.start (); } classMynewthread extends thread{@Override Public voidrun () {Try{sleep ( the); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } newsinfo="October 20, Jay Chou Royal as a poet Fang Wenshan voice in Weibo, Jay Chou's 13th album of all the songs have been recorded,"+"The album will include more than 10 songs and is expected to be released in December. Netizens have expressed expectations of "small 13 waiting is too difficult." "+"There are some netizens before Jolin [Weibo] new album "Pooh" Ridicule: Jay new album name is not << love yuck not yuck >>?"; Message Message=Handler.obtainmessage (); Bundle Bundle=NewBundle (); Bundle.putstring ("message", Newsinfo); Message.setdata (bundle); Handler.sendmessage (message); } } PrivateHandler Handler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Progressdialog.dismiss (); Shownewstextview.settext (Newsinfo); } }; @Override Public voidonconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig); LOG.I (TAG,"----onconfigurationchanged---"); } @Overrideprotected voidOnStart () {Super.onstart (); LOG.I (TAG,"----OnStart---"); } @Overrideprotected voidOnrestart () {Super.onrestart (); LOG.I (TAG,"----Onrestart---"); } @Overrideprotected voidOnresume () {super.onresume (); LOG.I (TAG,"----Onresume---"); } @Overrideprotected voidOnPause () {super.onpause (); LOG.I (TAG,"----OnPause---"); } @Overrideprotected voidOnStop () {super.onstop (); LOG.I (TAG,"----OnStop---"); } @Overrideprotected voidOnDestroy () {Super.ondestroy (); LOG.I (TAG,"----OnDestroy---"); } @Overrideprotected voidonsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate); LOG.I (TAG,"----onsaveinstancestate---"); }}
Run this appliction and then after sleep 5 seconds, show the news about "Jay Chou". Then, change the Phone to Landspace
LogCat to observe
This is the same as works, but we change the screen orientation not until the sleep time ends, we'll see
3.APP resources-handling Runtime Changes