Programming/Android

안드로이드#10 툴바 뒤로가기 버튼 만들기

Alxious 2020. 5. 11. 23:13

 

안드로이드 툴바에서 툴바 왼쪽에 뒤로가기 버튼 만들기

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

setDisplayHomeAsUpEnabled() ---> True 시 왼쪽에 뒤로가기 버튼 생성

 

뒤로가기 버튼 클릭

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home :
                finish();
        }
        return super.onOptionsItemSelected(item);
    }

 

추가하기

 

뒤로가기 버튼 ID 는 android.R.id.home 이다.