I'm migrating from ActionBar
to Toolbar
in my application.
But I don't know how to display and set click event on Back Arrow on Toolbar
like I did on Actionbar
.
With ActionBar
, I call mActionbar.setDisplayHomeAsUpEnabled(true)
.
But there is no the similar method like this.
Has anyone ever faced this situation and somehow found a way to solve it?
I used this method from the Google Developer Documentation:
If you get a null pointer exception it could depend on the theme. Try using a different theme in the manifest or use this alternatively:
Then in the manifest, where I set the parent activity for current activity:
I hope this will help you!
Easily you can do it.
Credits: https://freakycoder.com/android-notes-24-how-to-add-back-button-at-toolbar-941e6577418e
This worked perfectly
There are many ways to achieve that, here is my favorite:
Layout:
Activity:
If you are using an
ActionBarActivity
then you can tell Android to use theToolbar
as theActionBar
like so:And then calls to
will work. You can also use that in Fragments that are attached to
ActionBarActivities
you can use it like this:If you are not using
ActionBarActivities
or if you want to get the back arrow on aToolbar
that's not set as yourSupportActionBar
then you can use the following:If you are using
android.support.v7.widget.Toolbar
, then you should add the following code to yourAppCompatActivity
:And for API 21+
android:navigationIcon