当活动转到后台adapter.notifyDataSetChanged()时,不起作用

时间:2016-01-29 11:22:13

标签: android broadcastreceiver adapter

在我的应用程序中,单击thumbnial下载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(意向);

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

请修改你的onResume()方法

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