3

I am developing an app in which I have a fragment "A" and fragment "B" and I have a Navigation drawer in fragment "A". what I want when I switch to fragment "B" I want navigation drawer Icon gonna change to back Icon.How can I do that pls tell.

Raghvender Kumar
  • 143
  • 3
  • 11
  • what have you tried so far? Anyway I'd suggest to move the NavigationDrawer in another fragment (let's say "C") and then make the three fragments communicate via callbacks to their container activity – fredmaggiowski May 20 '16 at 09:07
  • See this http://stackoverflow.com/questions/36865463/manage-toolbars-navigation-and-back-button-from-fragment-to-fragment-in-android/36865576#36865576 – N J May 20 '16 at 09:10
  • 1
    Please post some code.. – AbhayBohra May 20 '16 at 09:16

2 Answers2

1

First: In your Activity (that one holding the toolbar and the home button) you need to create a boolean variable (a flag) to mark if the home button is the sandwich menu or the back arrow.

public class TheActivity {
  private boolean backButtonEnabled = false; //starting with the menu icon 
  ...
}

Second: In your Activity (that one holding the toolbar and the home button) you need to create a method enableBackButton(boolean enable) that will change the toolbar home icon to the back arrow if enabled is true or the sandwich menu if enabled is false and set the according boolean flag.

public class TheActivity {
  ...
  public void enableBackButton(boolean enable) {
    this.backButtonEnabled = enable;
    if(enable) {
      toolbar.setNavigationIcon(backIcon);
    } else {
      toolbar.setNavigationIcon(sandwichIcon);
    }
  }
}

Third: In fragment B (and in other fragments if you might) you should call getActivity().enableBackButton(true/false) when you reach onCreateView() and onDestroyView(). At this point, if you run the app, you will be able to change fragments and see the home button changing without action set.

public class FragmentB {

  @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
    ((TheActivity)getActivity().enableBackButton(true);
    ...
  }

  @Override
  public void onDestroyView() {
    ((TheActivity)getActivity().enableBackButton(false);
    super.onDestroyView();
  }
}

Last: you need to treat the home button click in onOptionsItemSelected(MenuItem item) depending on that flag backButtonEnabled you set when the button changed.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()){
    case android.R.id.home: {
      if(backButtonEnabled) actionBack(); //desired back action
      else actionSandwich(); //open drawer
      break;
    }
  }
  return super.onOptionsItemSelected(item);
}

I hope this find you well and helps with your app! Regards!

Maurício Fonseca
  • 1,006
  • 1
  • 9
  • 9
1

First: in your fragment add this two lines in onCreateView

    ((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);

Second: in your activity add this function to add fragment

public void addFragment(String title, Fragment fragment) {
    setToolbarTitle(title);
    FragmentUtils.addFragment(this, fragment,
            fragmentContainer, true, null);
    hideToolbarHamburger(true);
}

and remember to set the hamburger back in onBackPressed

@Override
public void onBackPressed() {
    if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
    }
    hideToolbarHamburger(false);
}

Third: and here comes the magic add this code as mentioned here

public void hideToolbarHamburger(boolean show) {
    if(show) {
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        actionBarDrawerToggle.setDrawerIndicatorEnabled(false);

        if(!mToolBarNavigationListenerIsRegistered) {
            actionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });

            mToolBarNavigationListenerIsRegistered = true;
        }

    } else {
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
        actionBarDrawerToggle.setToolbarNavigationClickListener(null);
        mToolBarNavigationListenerIsRegistered = false;
    }
}
Hasan Mhd Amin
  • 220
  • 2
  • 11