0

I am trying to pass a boolean value to a map fragment so that location updates will be requested only when this boolean value is false.

The value of this boolean value changes on the event of a button click.

RequestLocationUpdatesListener (interface)

public interface RequestLocationUpdatesListener {
        void onRequestLocationUpdates(boolean recording);
    }

Requesting Location Updates (Main Activity)

 // Turn on location updates and pass to fragment
 mButtonRecord.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG_CONTEXT, "Start Recording clicked.");
                mRequestLocationUpdates = true; // boolean value
                mRequestingLocationUpdates = new RequestLocationUpdatesListener() {
                    @Override
                    public void onRequestLocationUpdates(boolean recording) {
                        recording = mRequestLocationUpdates;
                        mRequestingLocationUpdates.onRequestLocationUpdates(recording);
                    }
                };
            }
        });

Map Fragment

The Log.i is never accessed in my MapFragment, what am I missing?

@Override
    public void onRequestLocationUpdates(boolean recording) {
        Log.i(TAG_CONTEXT, "Recording? = " + recording);
    }
user2324937
  • 115
  • 3
  • 12
  • 1
    possible duplicate of [Passing data between a fragment and its container activity](http://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) – Willis Feb 10 '15 at 00:03
  • @user2324937 you don't need to pass a value to your map fragment cause it will already have the location of the user – Hades Feb 10 '15 at 00:15

1 Answers1

0

Your problem is that you a creating a brand new listener in your onClick event everytime, which goes nowhere.

You need to have your mRequestingLocationUpdates variable pointing to your fragment that implements your interface. So, wherever you create your MapFragment you need to set this variable to the MapFragment.

For example, if you create your MapFragment in onCreate then it would go as follows:

@Override
protected void onCreate() {
    // Other code

    MapFragment mapFrag = new MapFragment();
    // Now register your fragment as the listener
    mRequestingLocationUpdates = mapFrag;

    // Other code.. 
}

Now you have a reference to your fragment and can call this from your onClick event like so:

// Turn on location updates and pass to fragment
 mButtonRecord.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d(TAG_CONTEXT, "Start Recording clicked.");
        mRequestLocationUpdates = true; // boolean value

        mRequestingLocationUpdates.onRequestLocationUpdates( mRequestLocationUpdates );
    }
});
Rob
  • 305
  • 1
  • 8