title: Android adding fragment in drawer layout
body: <p>I am having some difficulties when trying to use a navigation drawer and fragment together.</p>
  
  <p><strong>main.xml</strong></p>
  
  <pre><code>    &lt;LinearLayout 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=".GoogleMap" &gt;
  
      &lt;android.support.v4.widget.DrawerLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;!-- The main content view --&gt;
      &lt;FrameLayout
          android:id="@+id/content_frame"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
      &lt;!-- The navigation drawer --&gt;
      &lt;ListView android:id="@+id/left_drawer"
          android:layout_width="240dp"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:choiceMode="singleChoice"
          android:divider="#666"
          android:dividerHeight="1dp"
          android:background="#333"
          android:paddingLeft="15sp"
          android:paddingRight="15sp"
          /&gt;
  
      &lt;fragment
          android:id="@+id/map"
          android:name="com.google.android.gms.maps.SupportMapFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
      &lt;/android.support.v4.widget.DrawerLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>So the problem that I am facing is the navigation drawer only shows the content for a short moment when I rotate the device either from portrait to landscape or the other way around. The navigation drawer will remains empty if I do not tilt the device. </p>
  
  <p>The fragment is basically a so-called div to show a map. Then when I click on the navigation drawer, it will be on the top of the map fragment. I wonder which part of my layout went wrong and causing this problem.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>Edited Portion</strong></p>
  
  <pre><code>&lt;android.support.v4.widget.DrawerLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"&gt;
  
      &lt;fragment
      android:id="@+id/map"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent" /&gt;
  &lt;/LinearLayout&gt;
  
  &lt;!-- The navigation drawer --&gt;
  &lt;ListView android:id="@+id/left_drawer"
      android:layout_width="240dp"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:choiceMode="singleChoice"
      android:divider="@android:color/transparent"
      android:dividerHeight="0dp"
      android:background="#111"/&gt;
  
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>GoogleMap.java</p>
  
  <pre><code>public class GoogleMap extends FragmentActivity {
  
      // Google Map
      private com.google.android.gms.maps.GoogleMap map;
  
      private String[] drawerListViewItems;
      private DrawerLayout drawerLayout;
      private ListView drawerListView;
      private ActionBarDrawerToggle actionBarDrawerToggle;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          try {
              // Loading map
              initilizeMap();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          // get list items from strings.xml
          drawerListViewItems = getResources().getStringArray(R.array.items);
          // get ListView defined in activity_main.xml
          drawerListView = (ListView) findViewById(R.id.left_drawer);
  
          // Set the adapter for the list view
          drawerListView.setAdapter(new ArrayAdapter&lt;String&gt;(this,
                  R.layout.drawer_listview_item, drawerListViewItems));
  
          // App Icon 
          drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  
          actionBarDrawerToggle = new ActionBarDrawerToggle(
                  this,                  /* host Activity */
                  drawerLayout,         /* DrawerLayout object */
                  R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                  R.string.drawer_open,  /* "open drawer" description */
                  R.string.drawer_close  /* "close drawer" description */
                  );
  
          // Set actionBarDrawerToggle as the DrawerListener
          drawerLayout.setDrawerListener(actionBarDrawerToggle);
  
          getActionBar().setDisplayHomeAsUpEnabled(true); 
  
          // just styling option add shadow the right edge of the drawer
          drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
          drawerListView.setOnItemClickListener(new DrawerItemClickListener());
      }
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
          actionBarDrawerToggle.onConfigurationChanged(newConfig);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
  
           // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
          // then it has handled the app icon touch event
          if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      protected void onPostCreate(Bundle savedInstanceState) {
          super.onPostCreate(savedInstanceState);
          // Sync the toggle state after onRestoreInstanceState has occurred.
           actionBarDrawerToggle.syncState();
      }
  
      private class DrawerItemClickListener implements ListView.OnItemClickListener {
          @Override
          public void onItemClick(AdapterView parent, View view, int position, long id) {
              Toast.makeText(GoogleMap.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();
              //Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr=1.3564 ,103.8311&amp;daddr=1.3426 ,103.9254"));
              //startActivity(i);
              drawerLayout.closeDrawer(drawerListView);
  
          }
      }
  
      /**
       * function to load map. If map is not created it will create it for you
       * */
      private void initilizeMap() {
          if (map == null) {
              map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                      R.id.map)).getMap();
  
              map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(1.32814,103.80679) , 11.0f) );
  
              // check if map is created successfully or not
              if (map == null) {
                  Toast.makeText(getApplicationContext(),
                          "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                          .show();
              }
          }
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          initilizeMap();
      }
  }
  </code></pre>