Joe Stephan Joe Stephan - 2 months ago 16
Android Question

Android app - Back button

I want to display a back button in the left corner of the action bar and I do not completely know where to make the change in the activity.java to make it visible and functional.

@Override
protected void onCreate(Bundle savedInstancesState){
super.onCreate(savedInstancesState);
setContentView(R.layout.activity_news);
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.mipmap.ic_launcher);


This is what I want to display.
View Image

Answer

you have to enable it by doing

actionBar.setDisplayHomeAsUpEnabled(true);

then if you want it to do something you need to look for the click in the onOptionsItemSelected

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case android.R.id.home:
            //do something here
            break;
        default:
            break;
    }