ProgressBar不会重置为"0".第二次开放时 [英] ProgressBar does not reset to "0" when opening the second time

查看:332
本文介绍了ProgressBar不会重置为"0".第二次开放时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android应用程序中,我在单独的线程中运行任务.线程运行时,它将在主窗口中更新进度条,以使用户知道发生了什么.问题是,如果用户第二次启动线程,则进度条将不会被重置.它只会无所事事地坐在100%上.

In an Android application I run a task in a separate thread. While the thread is running it updates a progress bar in the main window to let the user know what's going on. The problem is, that if a user starts the thread a second time the progress bar won't be reset. It will just sit on 100% without doing anything.

整个来源是在github 上可用.例如,以 BackupThread JsonBackup (主类).请注意,"RestoreThread"显示相同的行为,但是我认为在两种情况下,问题都是相同的.

The whole source is available on github. As an example take the BackupThread and JsonBackup (the main class). Note that "RestoreThread" shows the same behaviour, but I assume that in both cases the problem is identical.

快速浏览:

大约

At around line 300 in JsonBackup.java, I open a new ProgressDialog. As it's a new instance, I assume all is initialised properly. On my current working copy I have added a line setting the progress to "0", but that didn't change anything. In the same block, I create the backup thread and run it.

下一个感兴趣的代码是线程将其进度返回到主应用程序的那一刻.这发生在 BackupThread.java的第185行.

The next bit of code of interest is the moment when the thread communicates it's progress back to the main application. This happens around line 185 of BackupThread.java.

该消息在 JsonBackup.java的第80行,它将更新进度条.

The message is handled in line 80 of JsonBackup.java which will update the progress bar.

到目前为止,有关代码演练.撰写本文时令我震惊的一件事是 JsonBackup.java 的第87行.我认为最好是线程发送不同的消息,明确指出已完成.但是我认为这不是真正的问题.我之所以这样说,是因为进度对话框在达到100%时会自动关闭.

So far for the code walkthrough. One thing that struck me while writing this is line 87 of JsonBackup.java. I think it would be best if the thread sent a different message, explicitly stating that it's done. But I assume this is not the real problem here. I say this because the progress dialog properly dismisses itself once it reaches 100%.

因此,如果整个过程都运行一次,那么一切都会按预期进行.现在假设用户离开了应用程序,但是操作系统决定将其加载到内存中.稍后,用户回来并想要创建一个新的备份.然后,该应用程序将无法工作,因为进度会以100%的速度挂起.如果您终止了该应用程序,则将其完全卸载,它将再次运行.因此,我假设某些对象实例仍在内存中,并且在重新运行时处于不干净状态.但是对于我的一生,我无法找到正在发生的事情.作为切线,我可能要补充:除了记事本教程,这是我的第一个Android应用程序,等等...

So, if this whole thing is run once, everything works as expected. Now assume, that the user leaves the application, but the OS decides to leave it loaded in memory. Later the user comes back and wants to create a new backup. Then the app won't work as the progress hangs at 100%. If you kill the app, so it's completely unloaded it will work again. So I assume that some object instance is still in memory and has an unclean state when re-run. But for the life of me, I cannot find what's going on. As a tangent I might add: Apart from the Notepad tutorials, this is my first Android app, and bla di blah blah...

我在这里想念什么...?

What am I missing here...?

推荐答案

ActivityActivity缓存,这是预期的行为.
选项是:

The Dialog gets cached by Activity, that's the expected behavior.
Options are:

  1. 覆盖onPrepareDialog(..)以重置其状态
  2. 在再次显示它之前先呼叫removeDialog(..)
  3. 亲自管理对话框,手动调用show()dismiss()
  1. override onPrepareDialog(..) to reset it's state
  2. call removeDialog(..) before showing it again
  3. manage the dialog yourself, manually calling show(), dismiss()

对于经常使用的Dialog s,我通常排在第一位;对于稀有的Dialog,我通常排在第三位.

I usually go #1 for frequently used Dialogs and #3 for the rare ones.

这篇关于ProgressBar不会重置为"0".第二次开放时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆