I have a fragment activity as seen in my last question:
App crashing when using fragments
It crushes because of a GoogleMap inside one of my fragments (MainActivity).
right now thats the code:
private GoogleMap map;
map = ((MapFragment) getActivity().getFragmentManager() .findFragmentById(R.id.map)).getMap();
I cant change GoogleMap. it causes other problems in my code as im using it.
What should I do?
Thanks for your assistance!
Ive tried to relocate that piece of code to onCreateView. Doesn't work either.
Related
I am struggling with animateCamera() method of the Google Maps. It only works on first launch of activity and if the activity is destroyed and created again, camera animations doesn't work but map is loaded fine. I have tried debugging the code, everything gets executed but map doesn't animate without any error or log. Although animation works when same mapFragment is used in the fragment but in activity it doesn't seem to work.
The solution mentioned in this this question is deprecated now and I am unable to fix this issue.
Map Fragment
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mapFragment.setRetainInstance(true);
#Override
public void onMapReady(GoogleMap googleMap) {
if (mMap == null) mMap = googleMap;
}
Code for animating camera
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, ZOOM_LEVEL));
Making entire project again from old code fixed the issue. But this issue re-appeared in another app. I found out that application was using old reference to mMap. This can be fixed by either getting reference from fragment manager or just setting mMap = null when activity is destroyed.
I have in my project an activity (that extends FragmentActivity), and two fragments (extends v4.fragment and one of them has a map fragment).
I made my project for tablet, and both fragments are shown on screen.
the problem is, I tried communicating between them (adding a marker on map from another fragment) , by using an Interface that is implemented in the activity.,
and for some reason my googleMap object returns null.
when I'm trying to use method inside the fragment
(e.g: options.mapType(GoogleMap.MAP_TYPE_SATELLITE)) using my googleMap object
it's working.
any idea what did i do wrong?
My bad. rookie mistake- I did'nt call the right fragment id from xml.... ( :
I have a viewpager where each "page" is a fragment. Each of the "page fragments" have a map fragment (google maps sdk for android) inside, added programatically using getSupportFragmentManager (If I use getFragmentManager it doesn't work either). Each added map is a new instance of SupportMapFragment (MapFragment doesn't work either).
On the first fragment, the one that is first added to the viewpager, the map works. At the other fragments, the non-interactive map just show itself as a grey square.
I'm using the 'com.google.android.gms:play-services-maps:7.5.0' dependency.
All the added maps are using the lite version. I'm doing this with:
GoogleMapOptions options = new GoogleMapOptions();
options.liteMode(true);
options.mapToolbarEnabled(false);
SupportMapFragment mapFragment = SupportMapFragment.newInstance(options);
I've read on some blogspots and here on Stackoverflow that maybe an app can only have one instance of google maps per process. Have anybody faced the same problem? Is my guess correct?
Thanks.
If you want to put a fragment into an other fragment, use getChildFragmentManager() instead of getSupportFragmentManager()
getChildFragmentManager
Return a private FragmentManager for placing and managing Fragments
inside of this Fragment.
As far as I can see, MapFragment has an issue with transition animations. All views on the layout are getting shown immediately, including the MapFragment's own views (like zoom buttons). But the map itself gets loaded with a lag only after the animation is completed.
In order to illustrate the problem, I did the following:
I changed one of the Activities in the Google maps android API examples slightly. It opens a blank activity via an Action Item. When I click back button, the map gets loaded, but only after the transition is completed.
I exaggerated the transition effect a little bit, so that you can see the problem better. I set the transition animation speed in Developer Options to 5x. Even on 1x speed, this lag is disturbing though.
See this video: http://www.youtube.com/watch?v=12SEotktlXI
Do you have any suggestion to prevent this lag? Why do all views get loaded immediately but the map itself doesn't?
Testing environment: Nexus 5, Android 4.4.2, unrooted
Edit: This problem also occures when MapView is used instead of MapFragment.
Reason: Its because as soon as you settings activity will be shown, the map activity will be on its onpause() state; thus, I assume android management reclaimed the memory from the map activity.
Solution:
Make a static class and declare you map statically there, to avoid android from reclaiming the memory used by your map.
Ex.
//your static class
public class MapData{
public static GoogleMap map;
}
//your map activity
public class MapActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(MapData.map != null)
MapData.map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
When the application opens I create a new instance of a google map during onAttachedToWindow(), this works fine while the application is open. When I close the application and re-open it the google map does not load correctly, it just shows grey tiles.
#Override
public void onAttachedToWindow() {
if (hasCreated()) {
uiGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_content_map_fragment)).getMap();
}
}
When should the google map be initialised to ensure it is loaded when the application is closed and re-opened?
EDIT:
I have tried initialising the map in onResume() and onCreate() and get the same problem
on Google API2 this hassle has been eliminated.
What you need to do is just initialize the map you created on your xml.
uiGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_content_map_fragment))
.getMap();
So when you close and re-open it it will be available. no need to override anything.
I think that the best place would be to create a class that extends SupportMapFragment, and initialise the map in:
#Override
public void onAttach(Activity activity)
Besides that though, I think if you initialise the map in onResume of the container activity it should be ok. But if you by initialising you mean adding the mapfragment to the activity, I would suggest doing it in the xml or in your activity's onCreate. Just my two pennies.