KaliMa KaliMa - 1 month ago 5
Android Question

How to implement back button feature when you have hamburger icon?

So in my app I have a section where a new Activity is started that contains a ViewPager / Navigation Drawer, the thing where you click the hamburger icon and a little pane slides out from the left so you have another place to click menu options.

However the button that causes this to slide out is the hamburger icon where normally the back arrow exists.

Is there a known way to get around this? I don't know if there is a feature that allows both a back arrow AND a hamburger/drawer-expansion icon on the screen at the same time in some form.

Answer

Normally you don't need to have a back arrow or button since Android has a back button built in with OS, which is also easy for users.

If you don't know how to handle it when Navigation Drawer is open, Here is the code.

@Override
public void onBackPressed() {
    if (isNavDrawerOpen()) {
        closeNavDrawer();
    } else {
        super.onBackPressed();
    }
}

Back arrow can't be placed along with hamburger icon in the toolbar. So, if you want back button in the UI, create a button and place it in the bottom. set OnClickListener and call onBackPressed() method inside it.

backButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            onBackPressed();
            }
    });
Comments