debo.stackoverflow
debo.stackoverflow

Reputation: 983

How to use a "LocalBroadcastManager" throughout the app?

I have a fragment showing a list of products. Each list item has a watchlist button showing a transparent heart icon if it is not watchlisted and a red heart icon if it is watchlisted.The user can click this heart icon in the listing page to watchlist or unwatchlist the product.

When clicking upon the body of an item, the user can go to the product details page. Here also there is a watchlist button.

The functionality I wanted is that when the user clicks the watchlist icon in the details page, it should also change the watchlist icon in the listing page when the user goes back from the details page to the listing page.

For this I used the local broadcast manager so that when the user clicks the watchlist icon in the details page, it sends a broadcast with the product id and the watchlist status. I just change the adapter json array data with the details send by the broadcast on to the receiver I wrote on the listing page and notify dataset changed.

My question is that when should I unregister this receiver which I wrote on the listing page?

I just cannot unregister the listing page broadcast receiver as I want to receive an event sent from the details page which is another page.

This is the receiver I wrote on the listing page.

final BroadcastReceiver broadcastReceiverWatchlist = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                try{
                    LogHelper.logReadyMadeString("aaaaaaaaa13");
                    String product_id = intent.getStringExtra("productId");
                    String watchlistStatus = intent.getStringExtra("wishlistStatus");
                    for(int i=0;i<totalProductJsonArray.length();i++){
                    if(totalProductJsonArray.getJSONObject(i).getString("product_id").equalsIgnoreCase(productId)){
                        totalProductJsonArray.getJSONObject(i).put("watchlist",watchlistStatus);
                          productListingAdaterRv.notifyDataSetChanged();
                          break;
                        }
                        }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };

LocalBroadcastManager.getInstance(mContext).registerReceiver(broadcastReceiverWatchlist, filter);

Here is the code I wrote on the details page to send a broadcast to the listing page

Intent intentToBroadCast = new Intent();

intentToBroadCast.setAction("com.applicationid.broadcast.WATCHLIST_NOTIFICATION");
intentToBroadCast.putExtra("wishlistStatus",watchlistStatus);
intentToBroadCast.putExtra("productId",productId);

LocalBroadcastManager.getInstance(mContext).sendBroadcast(intentToBroadCast);

The code is working fine. I watchlist a product in its details page. It sends a broadcast in the listing page and changes the watchlist status of the corresponding product id.

I want to know when if not should I unregister the receiver in the listing page?

Thanks for your time. If any doubt please inform me.

Note: The product listing page is a fragment in the home activity. The product details page is altogether a different activity.

Upvotes: 0

Views: 90

Answers (1)

JiajiaGu
JiajiaGu

Reputation: 1339

Just do unregister work in the onDestroy of the ProductListFragment

Upvotes: 1

Related Questions