Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

onMapReady never called when getMapAsync is posted #9923

Closed
kjkrum opened this issue Sep 6, 2017 · 2 comments
Closed

onMapReady never called when getMapAsync is posted #9923

kjkrum opened this issue Sep 6, 2017 · 2 comments
Labels
Android Mapbox Maps SDK for Android

Comments

@kjkrum
Copy link

kjkrum commented Sep 6, 2017

Using Mapbox Android SDK 5.1.2. I've extended SupportMapFragment. Originally I was calling getMapAsync in onCreate:

@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setHasOptionsMenu(true); // this is relevant
	if(savedInstanceState == null) {
		getMapAsync(new OnMapReadyCallback() {
			@Override
			public void onMapReady(final MapboxMap map) {
				// set up map
			}
		});
	}
}

The problem was that onMapReady was sometimes being called before the fragment's onCreateOptionsMenu, resulting in an NPE when I tried to access a menu item while setting up the map. So I tried posting the call to getMapAsync. When I do this, onMapReady is never called.

private final Handler mHandler = new Handler(Looper.getMainLooper());

@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setHasOptionsMenu(true); // this is relevant
	if(savedInstanceState == null) {
		mHandler.post(new Runnable() {
			public void run() {
				getMapAsync(new OnMapReadyCallback() {
					@Override
					public void onMapReady(final MapboxMap map) {
						// set up map
					}
				});
			}
		});
	}
}

So I tried posting from inside onMapReady. When I do this, everything works as expected.

private final Handler mHandler = new Handler(Looper.getMainLooper());

@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setHasOptionsMenu(true); // this is relevant
	if(savedInstanceState == null) {
		getMapAsync(new OnMapReadyCallback() {
			@Override
			public void onMapReady(final MapboxMap map) {
				mHandler.post(new Runnable() {
					public void run() {
						// set up map
					}
				});
			}
		});
	}
}

When checking for duplicate issues, I ran across #9136. I wonder if the fix for that created this.

@Guardiola31337 Guardiola31337 added the Android Mapbox Maps SDK for Android label Sep 6, 2017
@tobrun
Copy link
Member

tobrun commented Sep 11, 2017

Thank you for the report, will look into reproducing this. fwiw if you want to add additional logic I would rather advice not extending SupportMapFragment but extending SupportFragment instead and copying over the SupportMapFragment implementation.

re. #9136, the PR fixing that issue hasn't been released yet.
I have added the related milestone to the ticket to reflect it.

@tobrun
Copy link
Member

tobrun commented Jan 4, 2018

This has been fixed with #10717

@tobrun tobrun closed this as completed Jan 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Android Mapbox Maps SDK for Android
Projects
None yet
Development

No branches or pull requests

3 participants