关于android旋转屏幕时activity重建和android 转屏 onConfigurationChanged 不会执行的问题

参考 http://blog.csdn.net/tangzhilu/article/details/7399988

 

MainActivity 代码

复制代码
package com.example.configchangesample;

 
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    
    TextView textView1;
    String TAG = "configchangesample";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textView1 = (TextView)this.findViewById(com.example.configchangesample.R.id.textView1);
        textView1.setText("init");
        Log.i(TAG, "onCreate");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        textView1.setText("onConfigurationChanged");
        Log.i(TAG, "onConfigurationChanged:" + newConfig.orientation);
        
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            textView1.setText("ORIENTATION_LANDSCAPE");
        } else {
             
            textView1.setText("ORIENTATION_PORTRAIT");
        }
        
    }
}
复制代码

 

 

如果是 如下的配置

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.configchangesample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.configchangesample.MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
复制代码

在android 4.2.2 真机 和 android 2.3.5 真机验证,onConfigurationChanged 会被执行。

如果 android:configChanges="orientation|screenSize"  改为  android:configChanges="orientation" 

在android 4.2.2 真机上验证,onConfigurationChanged 不会被执行。

在android 2.3.5 真机上验证,onConfigurationChanged 会被执行。

 

去掉   android:targetSdkVersion="18"  并且   android:configChanges="orientation" 

在android 4.2.2 真机 和 android 2.3.5 真机验证,onConfigurationChanged 会被执行。

 

另外,

1.记得在手机上打开自动转屏的开关

2.测试发现, 转屏时,无论如何 onCreate 不会被触发。

网上的资料 加上keyboardHidden后onCreate 不会被触发,不加就会触发,这个解释貌似站不住脚。

3. 反正记得一点,如果设置了android:targetSdkVersion ,记得加上screenSize

4.值得注意的是当不加上android:configChanges = “screenSize”的时候是不会调用onConfigChange()方法而是执行activity的onCreate()方法

5.自从Android 3.2(API 13),screen size也开始跟着设备的横竖切换而改变。
所以,在AndroidManifest.xml里设置的MiniSdkVersion和 TargetSdkVersion属性大于等于13的情况下,
如果你想阻止程序在运行时重新加载Activity,除了设置"orientation",你还必须设置"ScreenSize"。
解决方法:
AndroidManifest.xml中设置android:configChanges="orientation|screenSize“

解释比较罗嗦,点击这里查看 http://developer.android.com/guide/topics/manifest/activity-element.html
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值