翻转设备时进度条会重置

我有一个小问题,可能不是最难解决的问题,到目前为止我什么都没找到:

在我的应用程序中,我使用连接到的进度条CountDownTimer当我翻转设备时,进度条会重置为0,而计时器不会重置,这会导致时间增长,而进度条仍位于中间位置。我为纵向/横向使用了两个不同的布局文件,但是进度栏的定义没有什么不同。

这是java代码:

mCountDownTimer=new CountDownTimer(6000,50) {

            @Override
            public void onTick(long millisUntilFinished) {
                Log.v("Log_tag", "Tick of Progress"+ i + millisUntilFinished);
                i++;
                mProgressBar.setProgress(i);

            }

            @Override
            public void onFinish() {
                i++;
                mProgressBar.setProgress(i);
                Toast toast = Toast.makeText(getApplicationContext(), "Time Up!", Toast.LENGTH_SHORT);
                toast.show();
                if(counter < DBAdapter.NUMBER_OF_QUESTIONS){
                    i=0;
                    newQuestion();
                }
                else endGame();
            }
        };

这是xml代码:

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:max="120"
    android:progress="0"
    android:visibility="visible" />

谢谢您的帮助!

编码PG

请注意,发生翻转时,您的活动将被销毁并重新创建。因此,局部变量中的值会丢失。

要避免丢失此类值,请使用onSaveInstanceState()onRestoreInstanceState()存储进度值,然后将其取回。

例子,

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("PROGRESS VALUE", i);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    i = savedInstanceState.getInt("PROGRESS VALUE");
}

您可以在这里阅读更多有关此的内容,

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

您可以在此处了解更多信息。

http://www.youtube.com/watch?v=2VYlTgaAHTs&list=PLlxmoA0rQ-LyCGSSD_nuPAmXDSR_FU0RR&index=31

http://www.youtube.com/watch?v=7XqHvJK9xn4&list=PLlxmoA0rQ-LyCGSSD_nuPAmXDSR_FU0RR&index=32

http://www.youtube.com/watch?v=4LYhbQXu19U&list=PLlxmoA0rQ-LyCGSSD_nuPAmXDSR_FU0RR&index=33

http://www.youtube.com/watch?v=W3NsvUX_Fwo&list=PLlxmoA0rQ-LyCGSSD_nuPAmXDSR_FU0RR&index=34

希望对您有所帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章