简体   繁体   中英

How to update fragment UI with data from SQLite in the onReceive() of Broadcast Receiver

I have a Firebase service that when new notification is in and the App is in the Foreground, I send a broadcast using a LocalBroadcastManager to fetch Data from SQLite and Update the Fragment UI(Recycler view). I have cross checked to see that the Broadcast Receiver is actually sending broadcasts as I can Log to see the string extras I added to the intent. I have an updateUI() method that will fetch results from SQLite and update the UI.

I have cross checked to see that my updateUI() works on its own.(If I called it in the onResume() even without the broadcast receiver).

My problem however is that updating the UI in the onReceive() of the Broadcast receiver doesn't seems to work.

Here is excerpts of my Fragment and how I implemented it.

public class NoticeListFragment extends Fragment{


    @Override
    public void onResume() {
        super.onResume();
        updateUI(); //In case data changes

        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mNoticeBroadcastReceiver,
            new IntentFilter("com.ultrasamad.htmcinside.NEW_NOTICE"));

    }

    @Override
    public void onPause() {
        super.onPause();

        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mNoticeBroadcastReceiver);
    }

    BroadcastReceiver mNoticeBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Check for intent filter
            if(intent.getAction().equals("com.ultrasamad.htmcinside.NEW_NOTICE")){
                //New notice has arrived, update UI
                String noticeTitle = intent.getStringExtra("message");
                Log.d(TAG, "Notice Title: " + noticeTitle);
                updateUI(); //Seems not to be working....
            }
        }
    };


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        //Inflate layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_notice_list, container, false);

        recyclerView = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
        recyclerView.setHasFixedSize(true);

        LinearLayoutManager linearManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearManager);

        updateUI();
        return rootView;
    }

    private void updateUI(){
        NoticeSource noticeSource = NoticeSource.get(getActivity());
        List<NoticeItem> notices = noticeSource.getNotices();

        if(mNoticeListAdapter == null){
            mNoticeListAdapter = new NoticeListAdapter(notices);
            recyclerView.setAdapter(mNoticeListAdapter);
        }else{

            mNoticeListAdapter.setNotices(notices);
            mNoticeListAdapter.notifyDataSetChanged();
        }

    }
}

In the onReceive() of Firebase Messaging where I created the broadcast.

Intent pushNotification = new Intent("com.ultrasamad.htmcinside.NEW_NOTICE");
                    pushNotification.putExtra("message", noticeTitle);
                    LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

Adapter class as requested.

private class NoticeListAdapter extends RecyclerView.Adapter<NoticeViewHolder>{

    private List<NoticeItem> listItems;

    private NoticeListAdapter(List<NoticeItem> data) {
        this.listItems = data;
    }


    @Override
    public NoticeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.notice_lists_card, parent, false);
        return new NoticeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(NoticeViewHolder holder, int position){

        final NoticeItem noticeItem = listItems.get(position);
        holder.bindNotice(noticeItem);

    }

    @Override
    public int getItemCount() {
        return listItems.size();
    }

    private void setNotices(List<NoticeItem> notices){
            listItems = notices;
    }
}
  1. check your parent activity of fragment . are u register that local broadcast receiver in activity then in fragment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM