
我在xml中定义了一个进度条(非常等待的样式),如下所示:
<Progressbar androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:layout_centerHorizontal="true" androID:ID="@+ID/progress" />@H_502_6@我使用以下方法将其可见性隐藏在活动的onCreate方法中:
progressbar.setVisibility(VIEw.GONE);@H_502_6@然后使用以下命令在按钮的onClick事件上启动它
progressbar.setVisibility(VIEw.VISIBLE);@H_502_6@现在,如果我更改屏幕装饰,进度条将消失.我了解该活动已根据方向更改被销毁并重新创建,并且该活动的状态已从保存的Bundle saveInstanceState中以新的方向重新创建.因此,我是否认为AndroID所保存的默认Bundle不包含对Progressbar VIEw对象所做的任何更改?
如果是这种情况,是否正确地说,改变方向后恢复Progressbar正确可见性的唯一方法是通过重写方法onSaveInstanceState并检查此标志来保存标志(例如boolean pbState = false / true).在onRestoreInstanceState并相应地设置可见性?或者,我是否错过了一些关于保存视图对象状态的真正明显的东西.
谢谢
更新:
下面提供的两种解决方案均有效.我决定选择将androID:configChanges =“ orIEntation | screenSize”放在清单xml文件中.但是,文档指出此方法仅应作为最后的手段使用.我的活动非常简单,因此清单xml方法减少了主要活动中所需的代码量,即没有onRestoreInstanceState方法.我想如果您的活动比较复杂,则可能需要使用后一种方法显式定义任何状态更改.
解决方法:
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或与此相关的任何其他小部件的状态.
[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):您可以执行此 *** 作而不会覆盖此方法.为了确认方向改变,请检查一下是否为savedInstanceState ==>.绑定传递给onCreate(Bundle)反对null.如果发生了方向更改,则saveInstanceState将不为null.在活动开始时,savedInstanceState将为null.以下代码(基本上是您所建议的)可以完成此工作:
声明一个全局布尔变量:
boolean progressbarIsShowing;@H_502_6@在您的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);}@H_502_6@每当您显示progressbar时,请将progressbarIsShowing设置为true.并在关闭progressbar时切换它.
覆盖onSaveInstanceState(Bundle):
if (progressbarIsShowing) { outState.putBoolean("progressbarIsShowing", progressbarIsShowing);}@H_502_6@注意:检查用户何时浏览远离您的活动(通过按下主屏幕按钮等).如果进度栏在用户显示时显示,则可能会收到BadTokenException.
总结以上是内存溢出为你收集整理的android-通过方向更改保持进度栏可见性全部内容,希望文章能够帮你解决android-通过方向更改保持进度栏可见性所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)