Android – How to avoid runtime exception when implementing home button to navigate back?

I faced problem when I tried to implement home button as back button.

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

That’s why I have removed these two lines, override onOptionsItemSelected method:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

Now it works without runtime exceptions!