【问题标题】:onConfigurationChanged() calling twice when change orientation Landscape to Portrait将方向横向更改为纵向时,onConfigurationChanged() 调用两次
【发布时间】:2013-02-14 06:42:09
【问题描述】:

我的应用出现了奇怪的问题。我问过here 的主要问题。问题是 android:configChanges="keyboardHidden|orientation" 在我的代码中不起作用。

所以我在我的代码中找到了通过@Override onConfigurationChanged() 方法管理它的解决方案来管理方向。但问题并没有得到妥善解决。

当前的问题是onConfigurationChanged() 在我们改变方向时调用了两次 横向转纵向。

如果我们将手机纵向更改为横向,它的变化和工作,但现在当用户将手机横向移动到纵向时,onConfigurationChanged() 将调用并返回相同的方向状态,在第二次调用中它将返回纵向。

代码:

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

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
             Log.e("On Config Change", "LANDSCAPE");

            Toast.makeText(getApplicationContext(), "L", Toast.LENGTH_LONG)
                    .show();
        } else 
        {
            Log.e("On Config Change", "PORTRAIT");
            Toast.makeText(getApplicationContext(), "P", Toast.LENGTH_LONG)
                    .show();
        }
    }

日志

first mode its port mode , so change in land mode 

02-28 12:10:06.274: E/On Config Change(540): LANDSCAPE 
02-28 12:10:14.154: E/On Config Change(540): LANDSCAPE
// here after changed the land mode try to chage in port mode then its calling two times as you can see as per the log 
02-28 12:10:14.593: E/On Config Change(540): PORTRAIT
02-28 12:11:39.524: E/On Config Change(540): LANDSCAPE

另一个查询相同的问题>>

当我们改变方向时(在调用onConfigurationChanged 时),它将终止当前活动。所以根据我之前的问题,我在不同的文件夹中有两个布局。因此,当我更改屏幕活动时,将删除所有数据。因此,无论如何,当用户更改手机方向时,我如何保存该数据以向用户显示。

【问题讨论】:

  • 您找到解决方案了吗?今天刚刚观察到同样的事情!

标签: android configuration tabs


【解决方案1】:

我不确定onConfigurationChanged() 是如何被调用两次的,但是为了避免在您的应用中出现错误行为,如果现在收到的方向与之前的方向相同,您可以取消onConfigurationChanged() 的执行。 为我工作,希望它也适合你:)

【讨论】:

  • android:configChanges="keyboardHidden|orientation|screenSize" onConfigurationChanged() 使用不同的参数调用了两次,在我们的例子中,一次用于方向,一次使用一些 screenSize 参数。您可以通过在日志中打印配置参数来检查为什么 onConfigurationChanged 被调用了两次。
猜你喜欢
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
相关资源
最近更新 更多