I'd like to know what is the correct approach to implement a MapFragment with a navigation drawer. I already had this working fine, but I have been reading some documentation and maybe it can cause troubles in the future.
So, my main class where the navigation drawer has been implemented is a very normal one, with the following selectItem()
method:
HomeActivity extends Activity{
//...
//Stuff like onCreate(), etc.
//...
private void selectItem(int position) {
if (position == 0) { //Position 0 is the map option in the navigationDrawer
Fragment fragment = new MapaFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
}
mDrawerList.setItemChecked(position, true);
setTitle(listaElementosDrawer[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
MapaFragment, which is the inflated fragment in the main activity, is as follows:
public class MapaFragment extends Fragment {
public MapaFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container,
false);
return rootView;
}
}
And, fragment_map.xml
which is inflated in MapaFragment
is like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.moictab.decanias.MapActivity$PlaceholderFragment" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
Notice that this is a mapFragment inside of a RelativeLayout, and it is already working well. But, if I remove the RelativeLayout and I left only the mapFragment, it crashes cause I'm inflating a fragment inside of a fragment. So, there is the questions:
I had a very simmilar problem with MapFragment. This is my solution:
I extend SupportMapFragment object:
public class MyMapFragment extends SupportMapFramgment
{
}
and add it programatically:
MyMapFragment fragment = new MyMapFragment();
fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).commit();
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