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);
}
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 );
}
});
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.