ProgressBarIndeterminateVisibility在更改轮换时重新出现

时间:2014-10-29 13:57:25

标签: android

我的应用有问题。如果我单击按钮在操作栏中停止ProgressBarIndeterminate,当我旋转屏幕时它会重新出现。我可以永远停止吗?感谢

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_main);

    setProgressBarIndeterminateVisibility(true);

}

public void test(View v) {
    setProgressBarIndeterminateVisibility(false);
}    

抱歉我的英文

3 个答案:

答案 0 :(得分:1)

当您旋转屏幕时,活动会再次创建(onCreate将再次被调用),因此您将调用setProgressBarIndeterminateVisibility(true),这将使其重新出现。

要跟踪您是否单击了停止按钮,您需要以某种方式保存状态(考虑实施onSaveInstanceState然后在onCreate中检索存储的值,或者保存陈述共享偏好/数据库)。

onSaveInstanceState - >当您的活动因轮换而被拆除时调用

private boolean showProgressBarVisibility = true; //always show the first time 

@Override
public void onCreate(Bundle savedInstanceState){

    if(savedInstanceState != null && savedInstanceState.containsKey("user3744384_pressed_the_button"){
       showProgressBarVisibility = false
    }

    setProgressBarIndeterminateVisibility(showProgressBarVisibility);
}

@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putString("user3744384_pressed_the_button", true);
}

答案 1 :(得分:0)

在onCreate中执行类似的操作:

if(savedInstanceState !=null){

 setProgressBarIndeterminateVisibility(false);
}

但你也应该在public void onSaveInstanceState(Bundle ...)中保存状态..研究活动如何保存他们的状态。

答案 2 :(得分:0)

当方向改变时,android会杀死你的活动并调用onCreate ..你可以保存你的应用状态并恢复它。

重写此方法以保存状态

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {

    savedInstanceState.putString(key, yourstatevariable);

}

并在onCreate中获取状态变量

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_one);

if (savedInstanceState != null) {

        String yourstatevariable = savedInstanceState.get(key);

    }

}