0

I have many Fragments all hosted by one Activity. Activity has ActionBar with Toolbar, DrawerLayout and menu_icon to open on close drawer. In one Fragment I show list of items, and when users clicks on one I am showing DetailFragment for that item.

What I want is to replace menu_icon when user is in DetailFragment to back_icon and set appropriate title, and when user clicks on this back_icon I want to pop that DetaiFragment from backstack and again show home_icon. So Click on home and back icons have to behave differently depending on current Fragment.

And I don't want to use Activity for DetailFragment. Is there is a way to manage ActionBar icons and actions in one place (hosting Activity)?

madim
  • 774
  • 10
  • 22
  • you can use your custom fragment toolbar which will replace the activity toolbar for the fragment. – Umair Nov 07 '17 at 08:25
  • @Umair you mean creating seperate ActionBar(Toolbar) for each Fragment? I think it will go below Activity's ActionBar(Toolbar) – madim Nov 07 '17 at 08:28
  • clear the menu in onCreateOptionsMenu and change hambruger icon then handle the homebackpress. – Arun Nov 07 '17 at 08:56

2 Answers2

1

I think you can just use getActivity() to get a reference to the activity. And then of course you can set the title with getActivity().setTitle("Details") and change the icon with getActivity().getActionBar().setIcon(R.drawable.back_icon).

Edit: You can also use this to enable the back button in your fragments. Place it in the onCreate() method. And you can use getSupportActionBar() for compatibility.

getActivity().getActionBar().setHomeButtonEnabled(true); getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

Linus
  • 111
  • 4
  • So I have to use these methods in every fragment? Is there is a way to manage it in one place (hosting Activity)? – madim Nov 07 '17 at 08:39
  • @madim I would make a parent fragment, place the code in its onCreate() method and have every fragment extend the parent fragment. – Linus Nov 07 '17 at 08:40
0

I found the answer here: https://stackoverflow.com/a/20314570/5222156

In onCreate on hosting Activity, after setting up Toolbar and Drawer I initialized listener for changes in backstack

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setupToolbar(R.drawable.ic_menu);
    setupNavigationDrawer();

    getSupportFragmentManager().addOnBackStackChangedListener(this);
}

The rest of the code looks almost the same as in the original answer:

@Override
public void onBackStackChanged() {
    shouldDisplayHomeUp();
}

public void shouldDisplayHomeUp() {
    //Enable Up button only  if there are entries in the back stack
    FragmentManager fragmentManager = getSupportFragmentManager();
    int count = fragmentManager.getBackStackEntryCount();

    Fragment fr = fragmentManager.findFragmentById(R.id.flContent);
    String tag = fr.getTag();

    boolean canGoBack = count > 0;
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setHomeAsUpIndicator(canGoBack ? R.drawable.ic_arrow_back : R.drawable.ic_menu);
        actionBar.setTitle(tag);
    }
}

@Override
public boolean onSupportNavigateUp() {
    boolean canGoBack = getSupportFragmentManager().getBackStackEntryCount() > 0;

    if (canGoBack) {
        navigationController.navigateBack();
    } else {
        mDrawerLayout.openDrawer(Gravity.START);
    }
    return true;
}

@Override
public void onBackPressed() {
    if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
        closeDrawer();
    } else {
        super.onBackPressed();
    }
}
madim
  • 774
  • 10
  • 22