Created
February 26, 2015 22:03
-
-
Save bdiegel/78f6227a54183653715b to your computer and use it in GitHub Desktop.
Android Toolbar Home Button
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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