通过方向更改保持进度栏可见性 [英] Maintaining Progress Bar Visibility with Orientation Change
问题描述
我在xml中定义了一个进度条(紧急等待样式),
I have a progress bar (swirly waiting style) defined in xml as:
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.Holo.ProgressBar.Large"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/progress"
/>
我使用
progressBar.setVisibility(View.GONE);
,并使用
progressBar.setVisibility(View.VISIBLE);
现在,如果我更改屏幕装饰,进度条将消失.我了解该活动已根据方向更改被销毁并重新创建,并且活动状态从保存的Bundle
savedInstanceState
以新的方向重新创建.那么,我是否认为Android保存的默认Bundle
不包括对ProgressBar
View对象所做的任何更改?
Now if I change the screen oreintation the progress bar disappears. I understand that the activity is destroyed and recreated on an orientation change, and the state of the activity is recreated in the new orientation from the saved Bundle
savedInstanceState
. So am I right in thinking that the default Bundle
saved by android does not include any changes made to to a ProgressBar
View object?
如果是这种情况,是否正确地说,更改方向后恢复ProgressBar
正确可见性的唯一方法是通过重写方法onSaveInstanceState
保存标志(例如boolean pbState = false/true
)并检查onRestoreInstanceState
中的此标志并相应地设置可见性?或者,我是否在保存视图对象的状态方面缺少真正明显的东西.
If this is the case, is it correct to say that the only way to reinstate the correct visibility of the ProgressBar
after an orientation change is to save a flag (e.g. boolean pbState = false/true
) by overriding the method onSaveInstanceState
and inspecting this flag in onRestoreInstanceState
and setting the visibility accordingly? Or, am I missing something really obvious about saving the state of view objects.
谢谢
下面提供的两种解决方案均有效.我决定选择将android:configChanges="orientation|screenSize"
放在清单xml文件中.但是,文档指出此方法仅应用作最后的选择.我的活动非常简单,因此manifest xml方法减少了主要活动中所需的代码量,即没有onRestoreInstanceState
方法.我想如果您的活动比较复杂,则可能需要使用后一种方法显式定义任何状态更改.
Both the solutions provided below work. I decided to opt for putting android:configChanges="orientation|screenSize"
in the manifest xml file. However, the documentation states that this method should only be used as a last resort. My activity is fairly simple, and so the manifest xml method reduces the amount of code required in the main activity, i.e., no onRestoreInstanceState
method. I presume if you're activity is more complex, you'll probably want to explicitly define any state changes using the latter method.
推荐答案
所以我在想这是android保存的默认Bundle吗? 不包含对ProgressBar View对象所做的任何更改?
So am I right in thinking that the default Bundle saved by android does not include any changes made to to a ProgressBar View object?
你是对的. Android不会保存progressBar
的状态或与此相关的任何其他小部件.
You are right. Android will not save the state of progressBar
, or any other widget for that matter.
[是]说恢复正确的唯一方法是正确的 方向更改后ProgressBar的可见性是保存一个 通过覆盖方法来标记(例如boolean pbState = false/true) onSaveInstanceState并检查onRestoreInstanceState中的此标志 并相应地设置可见性?
[Is] it correct to say that the only way to reinstate the correct visibility of the ProgressBar after an orientation change is to save a flag (e.g. boolean pbState = false/true) by overriding the method onSaveInstanceState and inspecting this flag in onRestoreInstanceState and setting the visibility accordingly?
绝对.关于onRestoreInstanceState(Bundle)
:您可以执行此操作而不会覆盖此方法.要确认方向改变,请对照null
检查savedInstanceState ==> Bundle passed to onCreate(Bundle)
.如果方向发生了变化,savedInstanceState
将不会为null.活动开始时,savedInstanceState
将为null
.以下代码(基本上是您所建议的)可以完成这项工作:
Absolutely. About onRestoreInstanceState(Bundle)
: You can do without overriding this method. To confirm orientation change, check for savedInstanceState ==> Bundle passed to onCreate(Bundle)
against null
. If an orientation change has occurred, savedInstanceState
will not be null. On start of an activity, savedInstanceState
will be null
. Following code (which is basically what you proposed) should do the job:
声明一个全局布尔变量:
Declare a global boolean variable:
boolean progressBarIsShowing;
在您的onCreate(Bundle)
中:
// savedInstanceState != null ===>>> possible orientation change
if (savedInstanceState != null && savedInstanceState.contains("progressbarIsShowing")) {
// If `progressBarIsShowing` was stored in bundle, `progressBar` was showing
progressBar.setVisibility(View.VISIBLE);
} else {
// Either the activity was just created (not recreated), or `progressBar` wasn't showing
progressBar.setVisibility(View.GONE);
}
每当显示progressBar
时,请将progressBarIsShowing
设置为true
.并在关闭progressBar
时进行切换.
Whenever you show progressBar
, set progressBarIsShowing
to true
. And toggle it when you dismiss progressBar
.
覆盖onSaveInstanceState(Bundle)
:
if (progressBarIsShowing) {
outState.putBoolean("progressBarIsShowing", progressBarIsShowing);
}
警告:检查用户何时浏览远离您的活动(通过按下主屏幕按钮等).如果在用户显示progressBar
时显示BadTokenException
,则可能会得到BadTokenException
.
Caution: Check for when user browses away from your activity(via home button press etc). You might get a BadTokenException
if progressBar
is showing when the user does so.
这篇关于通过方向更改保持进度栏可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!