Android.R.id.home cannot find character

I used the toolbar component in the AppCompat library instead of the default action bar.

Compilation time: I get a compilation error that cannot find the android.R.id.home symbol

public void setupActionBar() {
    // Set a Toolbar to replace the ActionBar.
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if(id == R.id.action_help) {
        showHelp();
        return true;
    }else if(id == android.R.id.home){
        Log.d(TAG, "Back Button clicked!");
        this.finish();
        return true;
    }

    return super.onOptionsItemSelected(item);
}
+4
source share
2 answers

android.R.id.home was introduced at API level 11. here in more detail: fooobar.com/questions/1622584 / ...

+2
source

add this to your activity.

  @Override
        public void onBackPressed() {
            // your code.
             this.finish();
}
+1
source

Source: https://habr.com/ru/post/1622580/


All Articles