Adding Google Map Fragment to an Activity - android

In my app, I only want to show the Google Map when a user taps a button. So, I created a subclass of SupportMapFragment and am now wanting to add that Fragment to my FragmentActivity using:
Fragment map = new ZSSGoogleMap();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.rootFrame, map);
fragmentTransaction.commit();
But, I keep getting an error on the first line telling me I need to cast it to ZSSGoogleMap.
What am I doing wrong here? I have worked with Fragments before and done exactly what I am trying to do here.

If you are using Fragments below API level 3.0 then you will have to use the Packages of v4.support for the Fragments and your Fragment will SupportFragment only.
As you are using SupportMapFragment so ,You will have to also use getSupportFragmentManager();

try this way if you want to use google map
MapFragment fragmentManager = getFragmentManager().findFragmentById(R.id.map);;
or if want to use support library then
SupportMapFragment fragmentManager = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap map= fragmentManager.getMap()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.rootFrame, map);
fragmentTransaction.commit();

Related

"Kill" fragment after replace it

I have a MainActivity with two tabs (using viewPager). In the second tab I have a mapFragment - Google Maps API. After clicking in a button it would open another fragment. However, the mapFragment is under the fragment (in log I see that the map is re-drawn).
Any suggestion how to solve it?
If you are using add() to go to the new fragment, replace it with replace().
See this example:
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, new NewFragment(), "NewFragment");
ft.commit();

Google maps v2 android with Action bar activity add and switch to another fragment

I have an app that is extending ActionBarActivity and is using google maps as fragment. I obtain the map like this:
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = fm.getMap();
So my question is, how to add another fragment and how to switch between them ? I've been googling and searching for solution but found nothing yet.
Use FragmentTransaction.replace() to replace fragments. Example:
Fragment newFragment = new MyFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.commit();
For more check this answer too.
If you are using a support package (v4, v7 or v11) or you are targeting API< 14 use getSupportgetSupportFragmentManager(). Else, for API>=14 use getFragmentManager().

Android Change main Fragment from child

I'm having some troubles with my Fragments management..
It's quite new for me and I'm pretty sure you'll find an easy way to solve my problem.
So I have a NavigationDrawer in a FragmentActivity and from this, I can navigate between two fragments.
One of those two fragments has children in a ViewPager.
I'd like to change the Parent Fragment from a ChildFragment which is in the ViewPager..
With my friend Paint, I drew my problem
http://goo.gl/OGdf1c
So as you can see, I'd like to load FragmentA from Fragment2B
Thank you !
Just perform a normal FragmentTransaction and be sure to use the FragmentManager of the Activity!
FragmentManager manager = getActivity().getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();
If your Activity is a FragmentActivity or ActionBarActivity you have to remember to use the support FragmentManager, aside from that everything stays the same:
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(...);
transaction.commit();

Android FragmentTransaction with fragment overlay

I have a actionbar tab setup with fragments for each of the pages. (extended default example from wizard).
I'm attempting to create a new fragment and make it the current fragment, with the other hidden
ConfigDetailSectionFragment configDetailSectionFragment = new ConfigDetailSectionFragment();
FragmentManager fragmentManager = getFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_main_config, configDetailSectionFragment);
transaction.addToBackStack(null);
transaction.commit();
the code shows the new fragment but seems it is transparent and I can see the fragment below.
i'm probably forgetting to do something... what am i forgetting to do or am missing?
Adding an android:background="#FFFFFF" fixed the issue, per beworker. Thanks!

Android: How to create a map by java code rather than by xml in google map api v2?

In my android project, I would like to create a Google Map using java code rather than useing xml, how should I do?
You can add a Map using MapFragment to an Activity in code. To do this, create a new MapFragment instance, and then call FragmentTransaction.add() to add the Fragment to the current Activity.
mMapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction =getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.my_container, mMapFragment);
fragmentTransaction.commit();
For more details check out the GoogleMaps, you will have more idea of implementing the maps.

Categories

Resources