简体   繁体   中英

How to use setDisplayHomeAsUpEnabled and fragment

I have an activity with 2 fragments and drawer. So when i am in fragment i get "Up" icon instead hamburger icon (with the help of setDisplayHomeAsUpEnabled) but the action is still the same - navigation_drawer_open/close. So how to get onBackPressed() instead?

And according to this comment i dont know how to handle the Home/Up by myself because of "automatically handle clicks".

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

Automatic handling usually only works between activities. Since you are using fragments you may need to handle them manually.

Here's an example on how to handle a back button press.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: // This is the home/back button
                onBackPressed(); // Handle what to do on home/back press
                break;
        }

        return false;
    }

For do this you must use Custom Toolbar and instead of android menu key use from this library :
material-menu


In this library you can change state for arrow and rotate -90 for show Up Icon instead of Burger icon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM