【问题标题】:When activity goes to background adapter.notifyDataSetChanged(), does not work当活动进入后台 adapter.notifyDataSetChanged() 时,不起作用
【发布时间】:2016-01-29 19:57:22
【问题描述】:

在我的应用程序中,点击缩略图会下载 pdf,progress indicator 显示下载进度,下载完成后会从下载的类中触发广播,并在主要活动中注册广播接收器并 adapter.notifyDataSetChanged() ;行动。进度指示器不可见,这是我的要求,但是 我的问题是,当主要活动进入后台并完成下载时,当用户返回活动时,进度指示器不会转到invisible.

BroadcastReceiver receiver=new BroadcastReceiver() 
{
@Override
 public void onReceive(Context context, Intent intent) {
 adapter.notifyDataSetChanged();
    }
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

     setContentView(R.layout.gridview);
    new DownloadJSON().execute();
}

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(receiver,new IntentFilter("com.download.complete"));
}

  @Override
public void onStop() {
    super.onStop();
    unregisterReceiver(receiver);

//下载pdf完成后,广播被触发 context.sendBroadcast(intent);

【问题讨论】:

    标签: android broadcastreceiver adapter


    【解决方案1】:

    您在 onStop() 方法中取消注册接收器。每当活动在后台进行时,您就没有监听接收器。因此,notifyDataSetChanged() 不会被调用,您的操作也不会被执行。

    【讨论】:

    【解决方案2】:

    请修改你的 onResume() 方法

    @Override
    protected void onResume() {
        if (adapter !=null) {
            adapter.notifyDataSetChanged();
        }
        registerReceiver(receiver,new IntentFilter("com.download.complete"));
        super.onResume();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 2013-11-18
      • 1970-01-01
      相关资源
      最近更新 更多