android学习---屏幕旋转

/**

*问题:今天学习android访问Servlet,Servlet给返回一个xml格式的字符串,android得到数据后将其显示到一个TextView中,发现Activity得到数据显

*   示到TextView后,如果旋转屏幕,Activity会回到初始状态,TextView中加载的内容销毁了,这在一个发布的应用中显然是不靠谱的

*原因:网上一堆搜以后,自己也实测了下,发现原来默认情况下屏幕旋转后,会调用Activity的onDestroy和onCreate方法,这样Activity就会重新加载

*     layout布局文件,一切回到原点,TextView中自然没数据了

*解决办法:在AndroidManifest.xml中给Activity加配置属性,黄色背景字段,添加该配置后就不会再调用onDestroy和onCreate方法,而是会调用

*       onConfigurationChanged方法以新的屏幕尺寸来设置这个Activity   

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

*/

/**测试截图**********************************************************************/

/**加了配置,(图中的create调用了是第一次调用,不是横屏的时候调用的,截图时疏忽了)*/

android学习---屏幕旋转

/**没有加配置**********************************************************/

android学习---屏幕旋转

/**来段源码,英文有限,意会即可****************************************************************/

android学习---屏幕旋转
/**
     * 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
        }
    }
android学习---屏幕旋转

android学习---屏幕旋转,布布扣,bubuko.com

android学习---屏幕旋转

上一篇:Oracle 11g R2性能优化 SQL TRACE


下一篇:移动IP技术之我见