应用放到android4.0终端,遇到了异常退出的问题.

2 篇文章 0 订阅
1 篇文章 0 订阅

【重现方法】

1. 在有些activity中横屏时,应用会异常退出。

 

【原因】

在这些activity的onCreate方法中使用了广播机制,

如:registerReceiver(m_oSDReceiver, getSdcardIntentFilter());

在横屏时会多次调用onCreate, 即多次注册了广播,导致死机。

 

【分析结果】

1. 如果你的sdk版本是2.3之前的,要做如下处理如下:

    1)在AndroidManifest.xml中,找到对应的activity添加: android:configChanges="orientation|keyboardHidden|locale

    2)  在activity 中重载onConfigurationChanged方法 

如果配置了android:configChanges这个属性,当我们横竖屏切换的时候会直接调用onConfigurationChanged方法,而不会重新执行onCreate方法,那当然如果不配置这个属性的话就会重新调用onCreate方法.

 

@Override


 


  public void onConfigurationChanged(Configuration newConfig){


 


   super.onConfigurationChanged(newConfig);


 


  if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){


 


  //横向


 


   }else{


 


  //竖向


 


   }


 


  }

 

 

 

2.  如果sdk是3.2以后的版本,需要添加 screenSize属性,如下:

    1) android:configChanges="orientation|keyboardHidden|locale|screenSize"

    2) 在activity 中重载onConfigurationChanged方法 

 

添加screenSize的原因:

"screenSize" The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

Added in API level 13.

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值