Manage toolbar's navigation and back button from fragment in android

前端 未结 8 2040
庸人自扰
庸人自扰 2020-12-02 10:00

All of my fragments are controlled through ActionBarActivity (mainActivity), inside mainActivity a DrawerLayout is implemented and all the child fr

8条回答
  •  隐瞒了意图╮
    2020-12-02 10:39

    First you add the Navigation back button

       getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    

    Then, add the Method in your HostActivity.

     @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==android.R.id.home)
        {
            super.onBackPressed();
            Toast.makeText(this, "OnBAckPressed Works", Toast.LENGTH_SHORT).show();
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    Try this, definitely work.

提交回复
热议问题