title: Extend MapFragment Android
body: <p>I have an activity that hosts two fragments.  One fragment provides some info to the user in various fields.  The other fragment should show a Map. </p>
  
  <p>For the fragment hosting the map I need to be able to add custom logic for displaying markers and several other things that I would like to reuse in other places in the app.  What is the best way to create a map without defining the fragment in xml like:</p>
  
  <pre><code>&lt;fragment
  class="com.google.android.gms.maps.MapFragment"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/&gt;
  </code></pre>
  
  <p>Thanks,
  Nathan</p>
  
  <p>Edit:</p>
  
  <p>I am now using nested fragments, not sure if that is the best way to handle this?  My mapfragment is now:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;fragment
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          class="com.google.android.gms.maps.MapFragment" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>and the Fragments code is:</p>
  
  <pre><code>public class ViewCurrentMapFragment extends Fragment implements View.OnClickListener {
      public static final String TAG = "MAP_FRAGMENT";
  
      private GoogleMap map;
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_view_current_map, container, false);
          return rootView;
      }
  
      private void getCurrentMarkers() {
          // Getting reference to the SupportMapFragment of activity_main.xml
          SupportMapFragment smf = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map));
  
          map = smf.getMap();
  
      }
  
      @Override
      public void onClick(View v) {
      }
  
  
      @Override
      public void onResume() {
          super.onResume();
          getCurrentMarkers();
      }
  
  }
  </code></pre>
  
  <p>The problem I am having now is that smf.getMap() is returning null.  Any ideas?</p>
  
  <p>Edit2:</p>
  
  <p>Got it to work by changing:</p>
  
  <pre><code>com.google.android.gms.maps.MapFragment
  
  to
  
  com.google.android.gms.maps.SupportMapFragment
  </code></pre>