Skip to content

Instantly share code, notes, and snippets.

@bdiegel
Created February 26, 2015 22:03
Show Gist options
  • Save bdiegel/78f6227a54183653715b to your computer and use it in GitHub Desktop.
Save bdiegel/78f6227a54183653715b to your computer and use it in GitHub Desktop.
Android Toolbar Home Button
# To add navigation back to the main activity using the
# Toolbar (material design), we do three things:
# 1. Declare the parent activity in the AndroidManifest.xml
#
# <activity android:name=".MySubActivity" android:label="@string/title_sub_activity" >
# <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" />
# </activity>
# 2. Configure the Toolbar in MySubActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
# add this to display the back arrow icon
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
# 3. Handle the selection event in MySubActivity:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
# other stuff ...
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment