语言切换,横竖屏切换,物理键盘推出导致activity重启原因分析和问题解决

  1. 测试那边 反馈的一个bug,这个问题一下子想起之前为了解决语言切换时采取重启桌面的方法。

    当时遇到 的是在进入桌面后切换语言,除了系统的activity会马上转变语言外,桌面上的图标文字等都没有马上更换语言,大概是纹理方面的事情,个中细节就不是很懂,但知道的是采取重启桌面的方式能比较好的解决问题,实现语言切换后 资源跟着更新。具体是采取通过在LauncherActivity中重载onConfigurationChanged()进行重启处理。

好了,问题来了, 这个bug让我想到应该不止语言更换会来到这个方法,因为Configuration这参数后边敲了一个点后提示还有不少属性。其中,就包括语言,屏幕状态, 键盘推出与否等属性,于是,在当初直接执行推出基础上加了判断,当只有语言变换时才执行推出,只是,经过测试反馈,问题还是存在,这又是为什么呢,推出键 盘后一定 会来到这个方法,可明明加了判断语句,常理来说绝对不会执行到重启语句才对。有点诡异。

好吧,google一下,还有点发现

------------------------------------分割线----------------------------------------------

If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes. 

Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called. 

To declare that your Activity handles a configuration change, edit the appropriate element in your manifest file to include the android:configChanges attribute with a string value that represents the configuration that you want to handle. Possible values are listed in the documentation for the android:configChanges attribute (the most commonly used values are orientation to handle when the screen orientation changes and keyboardHidden to handle when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe character ("|").

For example, the following manifest snippet declares an Activity that handles both the screen orientation change and keyboard availability change:


Now when one of these configurations change, MyActivity is not restarted. Instead, the Activity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your Activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your Activity.

------------------------------------
分割线----------------------------------------------

全部就 没看懂,但大概了解到一些,意识到问题其实根本不是出在那重启桌面的语句上面去,那怕没有任何语句,还是会发生 重启问题,这是因为当推开手机的实体键盘时,屏幕由竖屏转 换为横屏,此时应用程序的Activity就会被销毁了,是被销毁,所以为了让这个activity不被销毁才有了这个onConfigurationChanged()方法,提供一种屏幕方向改变时,应 用程序的显示界面也会随着改动,而不是被销毁的解决方案,但我重载了该方法,为什么还是会被销毁? 里边提到如果在该方法中不对改变的属性进行处理,例如,我就丝毫没有对见屏幕,键盘状态的改变出任 何处理,结果是activity照样会销毁。所以想要不被销毁,最好涉 及的属性都进行处理。行不行,明天找测试借机子测一下就知道了,目测可以。

你可能感兴趣的:(语言切换,横竖屏切换,物理键盘推出导致activity重启原因分析和问题解决)