Home » Android » Android Navigation Drawer Show Up Indicator for Lower Level Fragments

Android Navigation Drawer Show Up Indicator for Lower Level Fragments

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have some issues switching navigation drawer functionality to up functionality at lower level fragment. I have read this thread to manage to show the up indicator. But when pressing the button, it’ll open up the navigation drawer instead of going back to previous fragment. And I can’t set action bar title in EditUserFragment to “Edit Profile”.
I’m using the navigation drawer template available in Android Studio.

I have three levels:

  1. MainActivity with navigation drawer that consists of Home and Profile items
  2. UserFragment titled with “Profile” that has an option item that’ll bring up EditUserFragment
  3. EditUserFragment (lower level fragment) is triggered by UserFragment

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
            R.drawable.ic_launcher, R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    // this won't change the drawer indicator back
    drawerToggle.setDrawerIndicatorEnabled(true);
    // this works
    drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}

public void onSectionAttached(int number) {
    // Show the corresponding title on the action bar when clicked and open corresponding
    // fragments.
    Fragment fragment = null;

    switch (number) {
        case 1:
            mTitle = getString(R.string.title_home);
            break;
        case 2:
            mTitle = getString(R.string.title_profile);
            fragment = new UserFragment();
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.main, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

public void restoreActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setTitle(mTitle);
}

public void setActionBarTitle(String title){
    getActionBar().setTitle(title);
    Log.d("Title 2", getActionBar().getTitle().toString());
}

onBackPressed is working partially (commented in code) when I pressed back button on phone (not up button on action bar).


UserFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    setHasOptionsMenu(true);

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_profile, container, false);

    drawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
    drawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout,
            R.drawable.ic_launcher, R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);

    return view;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_edit:
            drawerToggle.setDrawerIndicatorEnabled(false);
            // Disable sliding from edge to open drawer
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, new EditUserFragment())
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                    .addToBackStack(null)
                    .commit();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

EditUserFragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
    ((MainActivity) getActivity()).setActionBarTitle("Edit Profile");
    Log.d("Title", getActivity().getActionBar().getTitle().toString());
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Get item selected and deal with it
    Log.d("KEY: ", String.valueOf(item.getItemId()));
    switch (item.getItemId()) {
        case android.R.id.home:
            Log.d("EditUserFragment", "I'm here");
            getActivity().onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); works as expected that up caret is showing instead of drawer; though when tapping it, it’s opening up drawer instead of going back to previous fragment. In addition, the code under case android.R.id.home is never executed.
And I try to set action bar title to “Edit Profile” from “Profile”. The log shows me “Edit Profile”, but the actual running app shows me “Profile” for some reason.


What I want to achieve is be able to go back to UserFragment from EditUserFragment by tapping the up indicator (right now the up indicator is opening up navigation drawer). And show correct title in EditUserFragment.

Any help is greatly appreciated!!

How to&Answers:

After days of analyzing, I discovered that the problem is I am using the built-in navigation drawer activity when creating it. The built-in separate the tasks into two. MainActivity and NavigationDrawerFragment. Thus, the drawerToggle I have in MainActivity is not the same as the real one in NavigationDrawerFragment.

Oroginally, MainActivty calls NavigationDrawerFragment to setup all the things needed for navigation drawer. I SOLVED this by implementing navigation drawer in my MainActivity, so I’ll have only one drawerToggle. Though, I still can’t find the way to make it work if I have everything (the navigation drawer variables) in NavigationDrawerFragment rather than MainActivity. If anyone knows the answer to that, feel free to leave a comment!