进度不重置为QUOT&0 QUOT;打开所述第二时间时 [英] ProgressBar not reset to "0" when opening the second time

查看:104
本文介绍了进度不重置为QUOT&0 QUOT;打开所述第二时间时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个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 。作为一个例子取<一个href=\"http://github.com/exhuma/android-contactbackup/blob/v1.0.1/src/lu/albert/android/jsonbackup/BackupThread.java\"相对=nofollow> BackupThread
 和<一个href=\"http://github.com/exhuma/android-contactbackup/blob/v1.0.1/src/lu/albert/android/jsonbackup/JsonBackup.java\"相对=nofollow> 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.

在通过一个快速步行:

在周围<一个href=\"http://github.com/exhuma/android-contactbackup/blob/v1.0.1/src/lu/albert/android/jsonbackup/JsonBackup.java#L295\"相对=nofollow>行300 JsonBackup.java ,我打开一个新的ProgressDialog。因为它是一个新的实例,我承担全部正确初始化。在我当前的工作副本我添加了一个线路设置进度为0,但这并没有改变任何东西。在同一个街区,我创建备份线程并运行它。

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.

利息code的下位是当下当线程通信,它的进步回主应用程序。出现这种情况各地<一个href=\"http://github.com/exhuma/android-contactbackup/blob/v1.0.1/src/lu/albert/android/jsonbackup/BackupThread.java#L185\"相对=nofollow> 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.

该消息在<一个处理href=\"http://github.com/exhuma/android-contactbackup/blob/v1.0.1/src/lu/albert/android/jsonbackup/JsonBackup.java#L80\"相对=nofollow>行JsonBackup.java 80将更新进度条。

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

目前为code演练。让我吃惊,而写这有一件事是<一个href=\"http://github.com/exhuma/android-contactbackup/blob/v1.0.1/src/lu/albert/android/jsonbackup/JsonBackup.java#L87\"相对=nofollow> 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应用程序,并BLA迪等等等等...

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...?

推荐答案

对话框得到由活动缓存,这是预期的行为。结果
选项​​包括:

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


  1. 覆盖上prepareDialog(..)重置它的状态

  2. 通话 removeDialog(..)再次展示它之前

  3. 管理自己的对话框中,手动调用显示()辞退()

  1. override onPrepareDialog(..) to reset it's state
  2. call removeDialog(..) before showing it again
  3. manage the dialog yourself, manually calling show(), dismiss()

我通常去#1经常使用对话框和#3罕见的。

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

这篇关于进度不重置为QUOT&0 QUOT;打开所述第二时间时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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