ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android-通过方向更改保持进度栏可见性

2019-10-12 13:29:06  阅读:185  来源: 互联网

标签:android bundle android-progressbar


我在xml中定义了一个进度条(非常等待的样式),如下所示:

<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"
        />

我使用以下方法将其可见性隐藏在活动的onCreate方法中:

progressBar.setVisibility(View.GONE);

然后使用以下命令在按钮的onClick事件上启动它

progressBar.setVisibility(View.VISIBLE);

现在,如果我更改屏幕装饰,进度条将消失.我了解该活动已根据方向更改被销毁并重新创建,并且该活动的状态已从保存的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;

在您的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时切换它.

覆盖onSaveInstanceState(Bundle):

if (progressBarIsShowing) {
    outState.putBoolean("progressBarIsShowing", progressBarIsShowing);
}

注意:检查用户何时浏览远离您的活动(通过按下主屏幕按钮等).如果进度栏在用户显示时显示,则可能会收到BadTokenException.

标签:android,bundle,android-progressbar
来源: https://codeday.me/bug/20191012/1900504.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有