3

Title is almost self explanatory.

Architecture:

  • I have Main activity that extends AppCompatActivity.
  • Also I have a drawer layout. Each option of this menu opens a fragment.
  • Each fragment has its own menu layout by setHasOptionsMenu(true) with its own buttons. The home button of their menus is still an access to open the drawer layout.
  • One of the fragments has a list of items and when one of these items is clicked a new child fragment is opened. (HERE IS THE PROBLEM) I want this child fragment to be able to navigate up to its parent by clicking on the up icon on its toolbar.

I disable the Drawer Toggle and set the Home As Up indicator just before replace the fragments:

        mDrawerToggle.setDrawerIndicatorEnabled(false);
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_white_24dp);
        actionBar.setDisplayHomeAsUpEnabled(true);

Then, when the UP icon is clicked, nothing happen. I debugged it and the onOptionsItemSelected method is not called.

FYI, all the other buttons that I added to the menu (search, refresh, etc) work and onOptionsItemSelected is called.

Diaviidi
  • 91
  • 8

1 Answers1

5

If you're implementing onOptionsItemSelected in the drawer activity, it will no longer be called in the fragment too, unless you return false in the activity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.home:
        return false; //The fragment will take care of it
    default:
        break;
    }
return false;
}

Hope it helps!

Update

You can also try starting a new activity and loading that fragment in the activity, as I said. Since you're already passing data through a Bundle to your child fragment, you can do the same for the activity.

Intent intent = new Intent(getActivity(), NewActivity.class);
Bundle bundle = new Bundle();
bundle.putWhatever(key, value); //There's no such method, just a generalization
intent.putExtras(bundle);
startActivity(intent);
  • Thanks for your suggestion, I have implemented onOptionsItemSelected method in MainActivity, ParentFragment and ChildFragment. I tried your proposal, setting MainActivity method to return false, but still not working – Diaviidi Nov 18 '15 at 17:27
  • FYI, all the other buttons that I added to the menu (search, refresh, etc) work and onOptionsItemSelected is called. – Diaviidi Nov 18 '15 at 17:31
  • 1
    Do you really need to use a child fragment there? Why not start a new activity and reuse that fragment you already wrote there? That would simplify your problem, I guess. – Razvan Alin Balan Nov 19 '15 at 09:01
  • Sorry I am still a newbie, why would I use another activity instead of a fragment? I have to pass data to the child fragment from the parent fragment and I managed to do that easily with fragments approach using bundle. – Diaviidi Nov 19 '15 at 15:43
  • I started a new activity and everything works there! Still don't know why it didn't work in the child fragment but changing to a new activity did the trick. – Diaviidi Nov 20 '15 at 09:44