how to override action bar back button in android?

前端 未结 10 708
孤街浪徒
孤街浪徒 2020-11-30 03:40

I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator

10条回答
  •  一生所求
    2020-11-30 04:18

    @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();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        //noinspection SimplifiableIfStatement
        if (id == R.id.signIn) {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    ///////////////////
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
    

提交回复
热议问题