Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues in using new Toolbar component,hiding and showing Navigation drawer icon ,Home Up icon click event from fragments

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.

Home Menu navigates to "Home Detail" through a button in it.

Layout of "MainActivity" :

<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">
<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <FrameLayout android:id="@+id/activity_frame" 
         android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <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" />
</android.support.v4.widget.DrawerLayout>

I am adding two fragments HomeFragment,CartFragment on "MainActivity" on menu selected from drawer as below, default selection is position 0 i.e "Home"

class "MainActivity.java" ::

@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<DrawerItem>();
    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()>0){      
           getSupportFragmentManager().popBackStack();      
           // show the drawer icon when on moving back 
           shouldDisplayHomeUp(false);         
        }else{
            super.onBackPressed();
        }
    mDrawerToggle.syncState();
}

My home fragment has button "Detail" on click of button,replaces to other fragment :"HomeDetailFragment"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
    android:id="@+id/btnDetail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Home detail" />
<FrameLayout
    android:id="@+id/home_frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

class "HomeFragment.java" btnDetail click event which replace to other fragment "HomeDetailFragment"::

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();
        }
});

class "HomeDetailFragment.java" ::

@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;
}

My problems ::

1. In "HomeDetailFragment", I can see the "UP home icon" but cannot get the click event of home icon onOptionsItemSelected not being called, so cant navigate back to HomeFragment

2. When pressing the Phone Back Button, and again navigating to "Home Detail" it is not showing "UP home icon"

Please guide me.

like image 630
Dory Avatar asked Dec 03 '14 11:12

Dory


2 Answers

  1. In "HomeDetailFragment", I can see the "UP home icon" but cannot get the click event of home icon onOptionsItemSelected not being called, so cant navigate back to HomeFragment

One way to achieve this is by using the Toolbar from your MainActivity like so:

Toolbar mToolbar= ((MainActivity)getActivity()).mToolbar;
mToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_nav_back));
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("MrE", "home selected");
            }
        });

  1. When pressing the Phone Back Button, and again navigating to "Home Detail" it is not showing "UP home icon"

The reason your code isn't getting called is because it is in the onCreate of your Fragment. Move it to the onCreateOptionsMenu instead to have it update.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Put toolbar stuff from above here
    super.onCreateOptionsMenu(menu, inflater);
}
like image 70
MrEngineer13 Avatar answered Sep 22 '22 03:09

MrEngineer13


I had the exact issue.

Use the setToolbarNavigationClickListener method.

In MainActivity.java,

actionBarToggle.setToolbarNavigationClickListener(new View.OnClickListener({
        @Override
        public void onClick(View v) {
            MainActivity.this.onSupportNavigateUp();
        }
    });

Avoid implementing Up Navigation individually in Fragments.

like image 20
user3316561 Avatar answered Sep 22 '22 03:09

user3316561