title: Issues in using new Toolbar component,hiding and showing Navigation drawer icon ,Home Up icon click event from fragments
body: <p>I recently started updating my app to use the new Toolbar component introduced in Android 5.0 with navigation drawer. Flow of app is : MainActivity which has a toolbar, navigation drawer with menu 1. Home  2. Cart.</p>
  
  <p>Home Menu navigates to "Home Detail" through a button in it.</p>
  
  <p>Layout of "MainActivity" :</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"
  android:orientation="vertical"&gt;
  &lt;include
      android:id="@+id/toolbar"
      layout="@layout/toolbar" /&gt;
  &lt;android.support.v4.widget.DrawerLayout
      android:id="@+id/drawer"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
      &lt;FrameLayout android:id="@+id/activity_frame" 
           android:layout_width="match_parent"
          android:layout_height="match_parent"/&gt;
      &lt;ListView
          android:id="@+id/left_drawer"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:background="@android:color/white"
          android:choiceMode="singleChoice"
          android:divider="@android:color/transparent"
          android:dividerHeight="0dp"
          android:listSelector="@android:color/transparent" /&gt;
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>I am adding two fragments HomeFragment,CartFragment on "MainActivity" on menu selected from drawer as below, default selection is position 0 i.e "Home"</p>
  
  <p>class "MainActivity.java" ::</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      mTitle = mDrawerTitle = getTitle();     
      drawer = (DrawerLayout) findViewById(R.id.drawer);
      mToolbar = (Toolbar) findViewById(R.id.toolbar);
      tvTitle = (TextView) mToolbar.findViewById(R.id.toolbar_title);
      if (mToolbar != null) {
          setSupportActionBar(mToolbar);          
      }       
      mDrawerToggle = new ActionBarDrawerToggle(this, drawer,mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
          public void onDrawerClosed(View view) {
              getSupportActionBar().setTitle(mTitle);
          }
  
          public void onDrawerOpened(View drawerView) {
              getSupportActionBar().setTitle(mDrawerTitle);
          }
      };
      drawer.setDrawerListener(mDrawerToggle);        
      mDrawerList = (ListView) findViewById(R.id.left_drawer);    
      dataList = new ArrayList&lt;DrawerItem&gt;();
      dataList.add(new DrawerItem(Constants.V_HOME, R.drawable.ic_launcher));
      dataList.add(new DrawerItem(Constants.V_MY_CART, R.drawable.ic_launcher));  
      mDrawerList.setAdapter(adapterDrawer);      
      mDrawerList.setOnItemClickListener(new DrawerItemClickListener());      
      // to show first "Home fragment" on start up of application
      if(savedInstanceState==null)
          SelectItem(0);
  }
  public void SelectItem(int position) {
      Fragment fragment = null;
      switch (position) {
      case 0:
          fragment = new HomeFragment();
          replaceFragment(fragment, Constants.V_TAG_HOME,false,"Home");
          break;
      case 1:
          fragment = new CartFragment();
          replaceFragment(fragment, Constants.V_TAG_MY_CART,false,"My Cart");
          break;
      }           
      drawer.closeDrawer(mDrawerList);
  }
  public void replaceFragment(Fragment fragment,String fragment_tag,boolean showHome,String title){   
      frgManager = this.getSupportFragmentManager();
      FragmentTransaction ft  = frgManager.beginTransaction();
      ft.replace(R.id.activity_frame,fragment,fragment_tag);      
      if(showHome)
          ft.addToBackStack(null);
      ft.commit();            
      shouldDisplayHomeUp(showHome);
  }
  public void shouldDisplayHomeUp(boolean showHome){
      if(showHome){
          mDrawerToggle.setDrawerIndicatorEnabled(false);
          drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
          getSupportActionBar().setHomeButtonEnabled(true);   
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      }else{
          mDrawerToggle.setDrawerIndicatorEnabled(true);
          drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
          getSupportActionBar().setDisplayHomeAsUpEnabled(false);
          getSupportActionBar().setHomeButtonEnabled(false);
      }
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {         
      getMenuInflater().inflate(R.menu.main, menu);
        return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {   
      switch (item.getItemId()) 
         {        
            case android.R.id.home:            
                 Toast.makeText(getApplicationContext(),"main act  clicked", Toast.LENGTH_SHORT).show();      
               return false;        
            default:            
               return super.onOptionsItemSelected(item);    
         }
  }
  @Override
  public void onBackPressed() {
      // TODO Auto-generated method stub      
      if(frgManager.getBackStackEntryCount()&gt;0){      
             getSupportFragmentManager().popBackStack();      
             // show the drawer icon when on moving back 
             shouldDisplayHomeUp(false);         
          }else{
              super.onBackPressed();
          }
      mDrawerToggle.syncState();
  }
  </code></pre>
  
  <p>My home fragment has button "Detail" on click of button,replaces to other fragment :"HomeDetailFragment" </p>
  
  <pre><code>&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;Button
      android:id="@+id/btnDetail"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Home detail" /&gt;
  &lt;FrameLayout
      android:id="@+id/home_frame_container"
      android:layout_width="match_parent"
      android:layout_height="match_parent" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>class "HomeFragment.java" btnDetail click event which replace to other fragment "HomeDetailFragment"::</p>
  
  <pre><code>btnDetail.setOnClickListener(new View.OnClickListener() {           
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
  
              Fragment fragment = new HomeDetailFragment();
              ((MainActivity)getActivity()).showDrawerIndicator(false);
              FragmentManager frManager = getActivity().getSupportFragmentManager();
              FragmentTransaction ft  = frManager.beginTransaction();
              ft.addToBackStack(null);
              ft.replace(R.id.activity_frame, fragment).commit();
          }
  });
  </code></pre>
  
  <p>class "HomeDetailFragment.java" ::</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setHasOptionsMenu(true);    
      // update the actionbar to show the up carat/affordance 
      ((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  }
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      // TODO Auto-generated method stub
      super.onCreateOptionsMenu(menu, inflater);
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {       
      switch (item.getItemId()) {
      case android.R.id.home:    
              // your code for order here
          Toast.makeText(getActivity(), "home detail", Toast.LENGTH_LONG).show(); 
          ((MainActivity)getActivity()).onBackPressed();          
       return true;
      }
      return true;
  }
  </code></pre>
  
  <p><strong>My problems ::</strong></p>
  
  <p><strong>1. In "HomeDetailFragment", I can see the "UP home icon" but cannot get the click event of home icon <code>onOptionsItemSelected</code> not being called, so cant navigate back to HomeFragment</strong></p>
  
  <p><strong>2. When pressing the Phone Back Button, and again navigating to "Home Detail" it is not showing "UP home icon"</strong></p>
  
  <p>Please guide me.</p>