How provide Up navigation with toolbar's home button on v7 toolbar
Did you have the below code to set your toolbar as the default activity actionbar?
setSupportActionBar(toolbar);
You did not set an image icon to the home button, maybe it shows up, but you just can't see it.
getSupportActionBar().setHomeAsUpIndicator(iconDrawable);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and as @Gonzalo said, you also have to override the menu select event to handle the home button onClick event
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//handle the home button onClick event here.
return true;
case android.R.id.other_menu
return true
}
return super.onOptionsItemSelected(item);
}
and why didn't you have a appbarlayout to contain the toolbar?
<android.support.design.widget.AppBarLayout
android:id="@+id/baseAppbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/baseToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
More actionbar implement details please have a look at my github project, hope it can help:
java code https://github.com/DanielShum/MaterialAppBase/blob/master/materialAppBaseLibrary/src/main/java/com/daililol/material/appbase/base/BaseActionbarActivity.java
xml code https://github.com/DanielShum/MaterialAppBase/blob/master/materialAppBaseLibrary/src/main/res/layout/base_actionbar_activity.xml
1- set your toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
2- setup your icon
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
3- override this method
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
It's very simple. Just do the following steps:
- Add the toolbar to the
activity_child.xml
:
(In my project, this AppBarLayout
is placed inside a ConstraintLayout
)
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
...
- Set up the toolbar in the
ChildActivity.java
public class ChildActivity extends AppCompatActivity
{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
...
}
- set the
parentActivityName
property inAndroidManifest.xml
<activity android:name=".ChildActivity"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".ParentActivity"/>
Important
For this to work properly, do not call finish()
in the parent activity after you call startActivity(intent)
.