Action bar Back button not working

前端 未结 13 1821
再見小時候
再見小時候 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条回答
  •  旧时难觅i
    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);
            }
    

提交回复
热议问题