Display back button on action bar
Display back button on action bar
Question
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
.
Popular Answer
well this is simple one to show back button
actionBar.setDisplayHomeAsUpEnabled(true);
and then you can custom the back event at onOptionsItemSelected
case android.R.id.home:
this.finish();
return true;
Read more... Read less...
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.
assert getSupportActionBar() != null; //null check
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //show back button
Step 2 implementation of back click: Override this method
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
thats it you are done
OR Step 2 Alternative: You can add meta to the activity in manifest file as
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />
Edit: If you are not using AppCompat
Activity then do not use support
word, you can use
getActionBar().setDisplayHomeAsUpEnabled(true); // In `OnCreate();`
// And override this method
@Override
public boolean onNavigateUp(){
finish();
return true;
}
Thanks to @atariguy for comment.
The magic happens in onOptionsItemSelected
.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Official solution
Add those two code snippets to your SubActivity
@Override
public void onCreate(Bundle savedInstanceState) {
...
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
add meta-data and parentActivity to manifest to support lower sdk.
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.SubActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
Reference here:http://developer.android.com/training/implementing-navigation/ancestral.html
Add these lines to onCreate()
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
and in onOptionItemSelected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Write your logic here
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Hope this will help you..!
Try this code, considers it only if you need the back button.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//YOUR CODE
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//YOUR CODE
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}