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
android android-fragments


source to share


No one has answered this question yet

Check out similar questions:

937
findViewById in fragment
823
onActivityResult is not called on fragment
740
Dilemma: when to use Fragments vs Activities:
702
How to detect when a fragment becomes visible in the ViewPager
608
Using context in a fragment
603
Best practice for instantiating a new Android fragment
436
How do I use LocalBroadcastManager?
4
Android: BatteryLevel widget not updating
2
How are getAssets in a service?
0
activityrecognition does not return activity every second



All Articles
Loading...
X
Show
Funny
Dev
Pics