Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a listview in a fragment from a broadcast receiver

I have to update a listview in a fragment if a file succesfully downloads. So in my download manager's BroadcastReceiver I register this new broadcast receiver:

Intent intent = new Intent();
intent.setAction("CONTENTS_NOTIFICATION");
context.sendBroadcast(intent);

And in my fragment in onCreateView I add the following code in order to register the receiver:

IntentFilter filter = new IntentFilter("CONTENTS_NOTIFICATION");
getActivity().getApplicationContext().registerReceiver(myReceiver, filter);

then:

private BroadcastReceiver myReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            SQLiteDatabase db = DatabaseManager.getInstance(getActivity()).getWritableDatabase();
              c = SelectedExperience.getSelectedExperiences(db);

              String id_esperienza = "Selected Experience";

              if (c.getCount() != 0) {
                  id_esperienza = c.getString(c.getColumnIndex(SelectedExperience.ID_ESPERIENZA));
              }

            populateListview(v, id_esperienza);

        }
    };

and at the end:

public void onDestroyView() {
      super.onDestroyView();
      getActivity().getApplicationContext().unregisterReceiver(myReceiver);
}

I have to add two of these broadcast receivers for two different Fragment. On the first one, all works fine. If in the application the user is on the fragment, it updates, while on the second fragment I got these errors:

03-15 07:32:40.474: E/AndroidRuntime(1692): FATAL EXCEPTION: main
03-15 07:32:40.474: E/AndroidRuntime(1692): java.lang.RuntimeException: Error receiving broadcast Intent { act=CONTENTS_NOTIFICATION flg=0x10 } in sample.actionscontentview.fragment.ContentsFragment$1@418e0850
03-15 07:32:40.474: E/AndroidRuntime(1692):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at android.os.Handler.handleCallback(Handler.java:725)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at android.os.Looper.loop(Looper.java:137)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at android.app.ActivityThread.main(ActivityThread.java:5191)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at java.lang.reflect.Method.invokeNative(Native Method)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at java.lang.reflect.Method.invoke(Method.java:511)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at dalvik.system.NativeStart.main(Native Method)
03-15 07:32:40.474: E/AndroidRuntime(1692): Caused by: java.lang.NullPointerException
03-15 07:32:40.474: E/AndroidRuntime(1692):     at sample.actionscontentview.fragment.ContentsFragment.populateListview(ContentsFragment.java:194)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at sample.actionscontentview.fragment.ContentsFragment.access$0(ContentsFragment.java:111)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at sample.actionscontentview.fragment.ContentsFragment$1.onReceive(ContentsFragment.java:107)
03-15 07:32:40.474: E/AndroidRuntime(1692):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758)
03-15 07:32:40.474: E/AndroidRuntime(1692):     ... 9 more

and the code is the same! I can't get why it's not working. It says there is a problem with populatelistView but the same function works fine inside the fragment if not called from the broadcast receiver.

The only difference I can think between this two fragments is that the one in which the update works is the first one opened when the application is launched.

like image 533
phcaze Avatar asked Mar 15 '13 06:03

phcaze


1 Answers

Try using EventBus by GreenRobot https://github.com/greenrobot/EventBus

When your file is succesfully downloaded you post an event

EventBus.getDefault().post(YourData.Downloaded);

Your fragment will register for receiving events, preferably in onStart() method

EventBus.getDefault().register(this);

And it will receive events in onEvent method:

public void onEvent(Data.State) {}

and then you can update your listview easily. You also unregister from receiving events in onStop() method. You can keep the data in your Application or stored. It is very easy and a nice solution. Hope it helps. You can register as many fragments as you want for this event so once the EventBus posts the event, all available fragments registered for it will receive this event.

like image 184
vandus Avatar answered Sep 27 '22 16:09

vandus