Action bar Back button not working

前端 未结 13 1764
再見小時候
再見小時候 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 03:59

    Use on SupportNavigateUp() method and call onBackPressed in this method.

    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 04:07

    onCreate

    {
        ...
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);
                resetActionBar();
        ...
     }
    
    
    public void resetActionBar()
        {
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }
    
     @Override
              public void onBackPressed() {
                FragmentManager fm = getSupportFragmentManager();
                int count = fm.getBackStackEntryCount();
                if(count == 0) {
                    // Do you want to close app?
                    showDialog();
                }else{
                    super.onBackPressed();
                }
            }
    
    
        @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.
                int id = item.getItemId();
                Log.i("coming", "comming");
                //noinspection SimplifiableIfStatement
                if(id==android.R.id.home){
                    if (getSupportFragmentManager().getBackStackEntryCount() > 0)
                        onBackPressed();
                    else
                        drawerLayout.openDrawer(navigationView);
                    return  true;
                }
    
                return super.onOptionsItemSelected(item);
            }
    
    0 讨论(0)
  • 2020-12-01 04:08

    Best and easy answer is add the parent activity name in Manifest file, so Actionbar back button will work.

    For that under that Activity tag of Manifest File use android:parentActivityName=".MyCustomParentActivity"

    0 讨论(0)
  • 2020-12-01 04:09

    You have just to add the following line to the activity in the manifest.xml. The parent activity is the activity to which you want to go back.

        android:parentActivityName=".activities.MainActivity"
    
    0 讨论(0)
  • 2020-12-01 04:09

    To me, I had to set mDrawerToggle.setToolbarNavigationClickListener(...) to a listener that triggers the back action. Otherwise it does nothing. This is what the source code of ActionBarDrawerToggle looks like:

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mDrawerIndicatorEnabled) {
          toggle();
        } else if (mToolbarNavigationClickListener != null) {
          mToolbarNavigationClickListener.onClick(v);
        }
      }
    });
    

    So the default behaviour is actually to call our listener, and not do any magic on its own.

    0 讨论(0)
提交回复
热议问题