我有一个处理程序注册在一个活动。 handleMessage()在适配器上调用notifyDataSetChanged。事情最初的重点在于事情的工作。但是,当我离开Activity并返回时,notifyDataSetChanged()不起作用。处理程序调用notifyDataSetChanged()不执行
FileAdapter是一个ArrayAdapter。 MergeAdapter是CommonsWare的一个自定义类。 _mergeAdapter包含_fileAdapter。
活动码:
public void setUpDownloadHandler() {
// Define the Handler that receives messages from the thread and update the progress
_downloadHandler = new Handler() {
public void handleMessage(Message message) {
super.handleMessage(message);
String fileId = (String) message.obj;
int progress = message.arg1;
FileInfo tempFile = null;
for (FileInfo file: _files) {
if (file.getFileId().equals(fileId)) {
file.setDownloadProgress(progress);
tempFile = file;
}
}
if (tempFile != null) {
_files.remove(tempFile);
_files.add(tempFile);
}
_fileAdapter.notifyDataSetChanged();
_mergeAdapter.notifyDataSetChanged();
}
};
}
跑过处理程序:
RunnableTask task = new DownloadFileRunnableImpl(application, the_workspace_url, the_file_info, the_workspace_info.getTitle(), the_internal_storage_directory,
_downloadHandler);
后台线程代码:
if(temp > previous) {
Message message = new Message();
message.arg1 = _currentProgress.intValue();
message.obj = _fileId;
_progressHandler.sendMessage(message);
previous = temp;
}
的另一条信息是我传递该处理程序通过一个活页夹,然后进入可运行。我这样做是为了在Service中运行后台线程。我不认为这是问题。
编辑: 好像处理程序不与活动相关联的是导航到(也许是因为的onCreate创建一个新的处理程序)的第二次。有没有办法重新关联或保留旧的处理程序?
更新 该活动正在被销毁,当它失去焦点到另一个活动。
确定。如果处理程序被销毁,它会不会丢失与后台线程的连接,而不会更新新的活动? – benkdev 2012-04-10 18:07:55
这就是我认为正在发生的事情。这可能也是别的,这就是为什么我认为你应该检查日志消息。或者你可以在调试模式下运行它,并在活动的onDestroy中放置一下,看看它是否被调用。 – flobacca 2012-04-10 18:14:07
我检查并且当我离开它时,活动不会被破坏。当我重新导航时,它会在onCreate()中创建一个新的处理程序。 – benkdev 2012-04-10 18:27:28