(4.6.12.7)Activity中ConfigChanges属性的用法

本文详细解释了如何在Android应用中利用android:configChanges属性来响应设备状态变化,包括字体缩放、MCC/MNC、地区变化、触摸屏、键盘模式、导航模式和设备方向切换。并通过实例展示了在AndroidManifest.xml中正确配置这些属性的方法,以避免不必要的生命周期回调。特别强调了从Android 3.2开始,为了阻止Activity重新加载,需要同时设置orientation和screenSize。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Activity中添加了 android:configChanges属性,目的是当所指定属性(Configuration Changes)发生改变时,通知程序调用 onConfigurationChanged()函数

public void onConfigurationChanged(Configuration newConfig) {  
                 super.onConfigurationChanged(newConfig);    
                 //TODO
                 domybusiness();  
    }    

类别

通过设置这个属性可以使Activity捕捉设备状态变化,以下是可以被识别的内容:

  1. CONFIG_FONT_SCALE
    全局字体大小缩放发生改变

  2. CONFIG_MCC
    MCC.移动国家号码,由三位数字组成,每个国家都有自己独立的MCC,可以识别手机用户所属国家

  3. CONFIG_MNC
    MNC.移动网号,在一个国家或者地区中,用于区分手机用户的服务商

  4. CONFIG_LOCALE
    用户所在地区发生变化

  5. CONFIG_TOUCHSCREEN
    This should never normally happen

  6. CONFIG_KEYBOARD
    键盘模式发生变化,例如:用户接入外部键盘输入

  7. CONFIG_NAVIGATION
    This should never normally happen.

  8. CONFIG_ORIENTATION
    设备旋转,横向显示和竖向显示模式切换。

实例

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="com.androidres.ConfigChangedTesting" 
      android:versionCode="1" 
      android:versionName="1.0.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <activity android:name=".ConfigChangedTesting" 
                  android:label="@string/app_name" 
                  android:configChanges="keyboardHidden|orientation"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
    </application> 
</manifest> 
import android.app.Activity;  
import android.content.Intent;  
import android.content.res.Configuration;  
import android.net.Uri;  
import android.os.Bundle;  
import android.provider.Contacts.People;  
import android.view.View;  
import android.widget.Button;  


public class ConfigChangedTesting extends Activity {  
    /** Called when the activity is first created. */  
    static final int PICK_REQUEST = 1337;  
    Button viewButton=null;  
    Uri contact = null;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //setContentView(R.layout.main);  

        setupViews();  
    }  


    public void onConfigurationChanged(Configuration newConfig) {  
                 super.onConfigurationChanged(newConfig);    


                 setupViews();  
    }    

    /* (non-Javadoc) 
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent) 
     */  
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        // TODO Auto-generated method stub  
        //super.onActivityResult(requestCode, resultCode, data);  


        if(requestCode == PICK_REQUEST){  


            if(resultCode==RESULT_OK){  


                contact = data.getData();  
                viewButton.setEnabled(true);  
            }  


        }  


    }  


    private void setupViews(){  


        setContentView(R.layout.main);  


        Button pickBtn = (Button)findViewById(R.id.pick);  


        pickBtn.setOnClickListener(new View.OnClickListener(){  


            public void onClick(View v) {  
                // TODO Auto-generated method stub  


                Intent i=new Intent(Intent.ACTION_PICK,People.CONTENT_URI);  
                startActivityForResult(i,PICK_REQUEST);  
            }  
         });  


         viewButton =(Button)findViewById(R.id.view);    


         viewButton.setOnClickListener(new View.OnClickListener() {  
                    public void onClick(View view) {  
                         startActivity(new Intent(Intent.ACTION_VIEW, contact));  
                     }  
         });    


         viewButton.setEnabled(contact!=null);  
     }  
 }  

注意

  1. 不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次

  2. 设置Activity的android:configChanges=”orientation”时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次

  3. 设置Activity的android: configChanges = “orientation | keyboardHidden”时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值