Toolbar back button is not calling onOptionsItemSelected Android

大憨熊 提交于 2019-11-28 11:50:34

问题


I can able to see the back button in the toolbar but when i click, nothing happens. It is not going to onOptionsItemSelected but when i remove the whole implementation of ActionBarDrawerToggle then the back button is working fine. I need to switch between both when i needed. Thank in advance.

package demo.sample.com.sample.base;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private DrawerLayout mDrawer;
    private ActionBarDrawerToggle mDrawerToggle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_digi_care);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this, mDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mDrawer.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();
        mDrawer.setFocusable(false);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setItemIconTintList(null);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.getMenu().getItem(0).setChecked(true);


        mDrawerToggle.setDrawerIndicatorEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Logger.i(TAG, "onOptionsItemSelected called");
        switch (item.getItemId()) {
            case android.R.id.home:
                Logger.i(TAG, "Back button pressed"); //Never getting called
                //onBackPressed();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.map_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

}

回答1:


If you want the onOptionsItemSelected() method to fire when the toggle Button is clicked, you need to use the four-parameter constructor for ActionBarDrawerToggle that doesn't take a Toolbar argument.

public ActionBarDrawerToggle(Activity activity,
                             DrawerLayout drawerLayout,
                             int openDrawerContentDescRes,
                             int closeDrawerContentDescRes)

Otherwise, the toggle will just handle the drawer opening/closing directly itself.




回答2:


And i finally got a solution. instead getting toolbar home button click ononOptionsItemSelected() it can be handled through DrawerToggle.setToolbarNavigationClickListener.

mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // event when click home button
                Log.d("cek", "home selected");
            }
        });

Thanks to @meow meo. Source - Cannot catch toolbar home button click event



来源:https://stackoverflow.com/questions/34903326/toolbar-back-button-is-not-calling-onoptionsitemselected-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!