Is getMapAsync() meant to run on a separate thread? I ask because on my application, when the map loads it appears to do it on the main thread because all UI is unresponsive until it loads. I am doing something wrong? Or am I meant to setup the background task myself?
The code below is my SupportMapFragment sub class. I pass a reference of the callback (onMapReadyCallback) which is implemented in my main activity to this class.
public class Map extends SupportMapFragment
{
private OnMapReadyCallback callback;
public static SupportMapFragment newInstance(OnMapReadyCallback callback)
{
Map map = new Map();
map.callback = callback;
return map;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
getMapAsync(callback);
}
@Override
public void getMapAsync(OnMapReadyCallback callback)
{
super.getMapAsync(callback);
}
And in my main activity, the map fragment is added.
FragmentManager fragmentManager = this.getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.map,Map.newInstance(this),Map.class.getName()).commit();
Is getMapAsync() meant to run on a separate thread?
No, it's not. Synchronous code would get executed at the point you're writing it. When writing asynchronous code you're saying what should happen when a certain event (map loaded) occurs some time in the future. Unless specified otherwise (like in AsyncTask) async execution does not imply using a separate thread.
Or am I meant to setup the background task myself?
Yes, but pay attention where to put the code.
I pass a reference of the callback (onMapReadyCallback) which is implemented in my main activity to this class.
Wrong. Any code related to the map should be contained within your map fragment (also maintain naming convention, even something stupid like MyMapFragment is way better than Map).
This means you supply input data via newInstance, implement an onMapReadyCallback inside the MyMapFragment (this callback is used primarily to modify the map markers, camera position and so on). And if you really need to let the outside Activity know, that the map is ready, you implement fragment callbacks in this standard way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With