android學習---屏幕旋轉

/**android

*問題:今天學習android訪問Servlet,Servlet給返回一個xml格式的字符串,android獲得數據後將其顯示到一個TextView中,發現Activity獲得數據顯web

*   示到TextView後,若是旋轉屏幕,Activity會回到初始狀態,TextView中加載的內容銷燬了,這在一個發佈的應用中顯然是不靠譜的佈局

*緣由:網上一堆搜之後,本身也實測了下,發現原來默認狀況下屏幕旋轉後,會調用Activity的onDestroy和onCreate方法,這樣Activity就會從新加載學習

*     layout佈局文件,一切回到原點,TextView中天然沒數據了測試

*解決辦法:在AndroidManifest.xml中給Activity加配置屬性,黃色背景字段,添加該配置後就不會再調用onDestroy和onCreate方法,而是會調用this

*       onConfigurationChanged方法以新的屏幕尺寸來設置這個Activity   spa

<activity  android:name="com.example.androidweb.xml.XmlActivity"
        android:label="@string/xml_parse"
       android:configChanges="orientation|screenSize">
</activity>

*/rest

/**測試截圖**********************************************************************/code

/**加了配置,(圖中的create調用了是第一次調用,不是橫屏的時候調用的,截圖時疏忽了)*/xml

/**沒有加配置**********************************************************/

/**來段源碼,英文有限,意會便可****************************************************************/

/**
     * Called by the system when the device configuration changes while your
     * activity is running.  
* (當activity運行時,你的設備屬性發生變化系統將會調用(該方法))
* Note that this will <em>only</em> be called if * you have selected configurations you would like to handle with the * {
@link android.R.attr#configChanges} attribute in your manifest.
* 須要注意的是,當且僅當你在manifest文件(AndroidManifest.xml)中配置了
android.R.attr#configChanges對
* 應的屬性(onconfigChanges屬性),該方法纔會被調用

* If
any configuration change occurs that is not selected to be reported
 * by that attribute, then instead of reporting it the system will stop * and restart the activity (to have it launched with the new * configuration). * 若是沒有配置該屬性,那麼當任何配置屬性發生變化時,系統將會中止並重啓該activity,而不是以新屬性從新配置activity * <p>At the time that this function has been called, your Resources * object will have been updated to return resource values matching the * new configuration. * * @param newConfig The new device configuration. */ public void onConfigurationChanged(Configuration newConfig) { if (DEBUG_LIFECYCLE) Slog.v(TAG, "onConfigurationChanged " + this + ": " + newConfig); mCalled = true; mFragments.dispatchConfigurationChanged(newConfig);//配置fragment if (mWindow != null) { // Pass the configuration changed event to the window  mWindow.onConfigurationChanged(newConfig);//有window對象,配置window } if (mActionBar != null) { // Do this last; the action bar will need to access // view changes from above.  mActionBar.onConfigurationChanged(newConfig);//有actionBar,配置actionBar } }
相關文章
相關標籤/搜索