Failed to get LocalBroadcastManager in fragment

I have an Activity that has an additional intent that needs to be passed to a Fragment using LBM.

Here's what I've done so far:

In a snippet. Please note that I am calling Activity from this fragment .:

public class HomeFragment extends Fragment {

    ...

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.nearbyLocation:
                Intent i = new Intent(getActivity(), NearbyLocationActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                i.putExtra("location_id", location_id);
                i.putExtra("hyperlocal", hyper_local);
                startActivity(i);

                break;
    }

    ...

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

        // Broadcast message from NearbyLocationActivity to this fragment
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("NearbyLocationEvent");
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
                mUpdateUIReceiver, intentFilter);
    }

    private BroadcastReceiver mUpdateUIReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equalsIgnoreCase("NearbyLocationEvent")) {
                Log.d(TAG, "intent message= " + intent.getStringExtra("lat"));
            }
        }
    };

    @Override
    public void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mUpdateUIReceiver);
    }


}

      

Activity:

public class NearbyLocationActivity extends FragmentActivity
        implements OnNearbyLocationClickedListener {

    ...

    @Override
    public void onNearbyLocationClicked(String lat, String lon, boolean reset_coord) {

        Log.d(TAG, "clicked= " + lat);

        Intent i = new Intent("NearbyLocationEvent");
        i.putExtra("lat", lat);
        i.putExtra("lon", lon);
        i.putExtra("reset_coord", reset_coord);
        LocalBroadcastManager.getInstance(this).sendBroadcast(i);
        finish();
    }
}

      

Also note that Fragment is not an instance of NearLocationActivity. From another action.

The problem is that the LBM is not called in the Fragment after onNearbyLocationClicked in the Activity, even though I have set the IntentFilter.

I want to get data from Activity to Fragment. How to fix it? Do I need to insert this internal manifest?

+3


source to share





All Articles