Action bar Back button not working

前端 未结 13 1819
再見小時候
再見小時候 2020-12-01 03:13

with the help of these Android Docs.I am trying to do a action bar Back button.I get an Action Bar Back Button like these below image:

13条回答
  •  星月不相逢
    2020-12-01 04:01

    None of the answers provided here worked for me. I had to put the switch inside the onMenuItemSelected method. I'm aware this is not what is stated in the Android documentation, but still, it worked, so I just thought I'd leave this here for people who run into the same issue. My problem involved an Activity instead of a Fragment though, but that should be pretty much the same.

    class FooActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // ...
    
            getActionBar().setHomeButtonEnabled(true);
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
        @Override
        public boolean onMenuItemSelected(int featureId, MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    NavUtils.navigateUpFromSameTask(this);
                    return true;
            }
    
            return false;
        }
    }
    

提交回复
热议问题