I'm trying to display a Back button
on the Action bar
to move previous page/activity or to the main page (first opening).
And I can not do it.
my code.
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
the code is in onCreate
.
Official solution
Add those two code snippets to your SubActivity
add meta-data and parentActivity to manifest to support lower sdk.
Reference here:http://developer.android.com/training/implementing-navigation/ancestral.html
On your
onCreate
method add:While defining in the
AndroidManifest.xml
the parent activity (the activity that will be called once the back button in the action bar is pressed):In your
<activity>
definition on the Manifest, add the line:To achieve this, there are simply two steps,
Step 1: Go to
AndroidManifest.xml
and add this parameter in the<activity>
tag -android:parentActivityName=".home.HomeActivity"
Example:
Step 2: In
ActivityDetail
add youraction
for previous page/activityExample:
I Solved in this way
In your onCreate method add this line
and add this method to your activity
I think
onSupportNavigateUp()
is the best and Easiest way to do so, check the below steps. Step 1 is necessary, step two have alternative.Step 1 showing back button: Add this line in
onCreate()
method to show back button.Step 2 implementation of back click: Override this method
thats it you are done
OR Step 2 Alternative: You can add meta to the activity in manifest file as
Edit: If you are not using
AppCompat
Activity then do not usesupport
word, you can useThanks to @atariguy for comment.