1

I have created a SupportMapFragment dynamically, because I have some nested fragments and doing it with a <fragment> was crashing my app (FragmentTabHost doesn't play nice with <fragment> when you change tabs).

Now I want to do a simpel getMap() call but I always get null. I have called it from onCreate and from onCreateView...so far I have researched, this is because the fragment is still not created, so probably doing it in onResume is not a sure bet either, since the

 getChildFragmentManager().beginTransaction().replace(R.id.map_frame, mMapFragment).commit();

is an Asynchronous call.

So my question is: How can I know that my Map is ready to be used? Is it there any listeners that can be used for this? Any common way of doing it? I can't find much in the docs about this.

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • possible duplicate of [How do I know the map is ready to get used when using the SupportMapFragment?](http://stackoverflow.com/questions/14047257/how-do-i-know-the-map-is-ready-to-get-used-when-using-the-supportmapfragment) – shkschneider Jul 03 '14 at 08:39

3 Answers3

0

Perhaps you are trying to obtain the map object before the fragment has finished being created. using a LocationClient may help you, as mentioned by this gentleman here.

One would call locationClient.connect(); to establish connection. Upon a successful connection the onConnected method is executed. If it fails, onConnectionFailed is called.

Google's example or this example should help you along.

Community
  • 1
  • 1
user2818782
  • 736
  • 8
  • 18
0

I had this issue yesterday, and using this method

https://stackoverflow.com/a/15303445/3309883

Community
  • 1
  • 1
Unknownweirdo
  • 485
  • 6
  • 16
0

After looking into the other answers I made a much simpler option myself:

mMapFragment = new SupportMapFragment() {
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View view = super.onCreateView(inflater, container, savedInstanceState);
                setupMapIfNeeded();
                return view;
            }
        };

This way, I extend the SupportMapFragment anonymously, and I don't need to create a new class or interfaces or listeners. Although if you need to reuse things and can benefit for more complex use cases, I recommend you to extend it in a new class and setup some listnets, but for my purpouse this was more than necessary.

Word of advice: Using it this way you can't use the factory method newInstance() of the SupportMapFragmet, so you will have to use the constructor, which for me is working just fine, but some advice to not use it.

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173