Moha the almighty camel
Moha the almighty camel

Reputation: 4453

Implementing Up button in toolbar - working without java code

I am learning android and I am playing around with up and back button.
According to the documentation implementing up requires the following:

1- declare parent activity in the manifest
2- getActionBar().setDisplayHomeAsUpEnabled(true); in onCreate
3- overriding onOptionsItemSelected and processing the case of android.R.id.home

The Up button seems to work fine just by doing step one. why are steps 2 and 3 necessary?

I am using AppCompat

Upvotes: 0

Views: 25

Answers (1)

foxanna
foxanna

Reputation: 1570

onOptionsItemSelected callback is called every time user touches any of toolbar icons, including those on the right.

overriding onOptionsItemSelected and processing the case of android.R.id.home

will let you check the id of pressed options button and react to user request by executing some code. In case the id is equal android.R.id.home, just return from onOptionsItemSelected method.

Here is an example from Android documentation.

Upvotes: 1

Related Questions