AppCompat v7 Toolbar onOptionsItemSelected not called

前端 未结 6 1797
感动是毒
感动是毒 2020-12-14 00:15

I changed from the original ActionBar to the AppCompat Toolbar and setSupportActionBar(toolbar). When I am using getSupportActionBar() and setDisplayHomeAsUpEnabled(true) fo

6条回答
  •  一个人的身影
    2020-12-14 00:55

    I know this question has been answered but I found the real cause of the problem after 2 days of frustration.

    Take a look at the ActionBarDrawerToggle documentation: https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html

    Notice the two constructors there. My mistake was that I was using the second constructor that was taking a toolbar as a parameter. It took me so long to notice the last line in the consturctor documentation: "Please use ActionBarDrawerToggle(Activity, DrawerLayout, int, int) if you are setting the Toolbar as the ActionBar of your activity."

    After using the first constructor onOptionsItemSelected() was called with no issues.

    Don't forget to call the ActionBarDrawerToggle.onConfigurationChanged() and onOptionsItemSelected() from your activity as described in the last part here: http://developer.android.com/training/implementing-navigation/nav-drawer.html

提交回复
热议问题