onBackPressed() method not triggered in AppCompatActivity

前端 未结 6 1041
囚心锁ツ
囚心锁ツ 2021-02-13 23:26

AppCompatActivity onBackPressed() method fails to trigger in my activity.

I see the back arrow button and get the animation when pressing it, but nothing else happens. a

6条回答
  •  不要未来只要你来
    2021-02-14 00:10

    I believe onBackPressed() is only called when the physical back button is pressed. If you're attempting to catch the toolbar back button press (the navigation icon), try using the following snippet:

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case android.R.id.home: {
                // Your code here
            }
        }
        return (super.onOptionsItemSelected(menuItem));
    }
    

提交回复
热议问题