I am making an app and I want to put an action bar with a back arrow in a fragment.
So, I already have the fragment with the action bar but don't know how to put the back arrow on it.
Can you help me please?
Thank you,
Guilherme
This is the fragment with an action bar tag

- 3,185
- 1
- 22
- 42

- 525
- 1
- 4
- 11
-
See http://stackoverflow.com/questions/13086840/actionbar-up-navigation-with-fragments answer – fbwnd Dec 04 '16 at 15:44
5 Answers
Add following line in your fragment if you want to show the back button from the fragment :
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
It would be better that along with doing this. You add the parent of the activity in the manifest file to make sure that parent activity is opened when back arrow is pressed.

- 5,760
- 5
- 28
- 44
I have some thing like this for back button in toolbar instead of action bar.
In activity_main.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wolfmatrix.dummy.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarId"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/toolbarTextId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="14sp" />
</android.support.v7.widget.Toolbar>
<ImageButton
android:id="@+id/backButtonIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="left"
android:padding="10dp"
app:srcCompat="@drawable/ic_arrow_back_black_24dp" />
</RelativeLayout>
In styles.xml
: use theme => Theme.AppCompat.Light.NoActionBar
In ic_arrow_back_black_24dp.xml
, use this:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#ffffff"
android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>
And now, toolbar has the back icon too.

- 3,747
- 4
- 27
- 53
-
when you say "In styles.xml: use theme => Theme.AppCompat.Light.NoActionBar", you have to create a new style tag, right? – Guilherme R Dec 05 '16 at 16:26
-
Yes, but if you want to use `ToolBar` instead of `ActionBar`, you have to replace the default one by `Theme.AppCompat.Light.NoActionBar` . – Satan Pandeya Dec 05 '16 at 16:31
-
Try below code
Implement OnBackStackChangedListener and add this code to your Fragment Activity.
@Override
public void onCreate(Bundle savedInstanceState) {
//Listen for changes in the back stack
getSupportFragmentManager().addOnBackStackChangedListener(this);
//Handle when activity is recreated like on orientation Change
shouldDisplayHomeUp();
}
@Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
boolean canback = getSupportFragmentManager().getBackStackEntryCount()>0;
getSupportActionBar().setDisplayHomeAsUpEnabled(canback);
}
@Override
public boolean onSupportNavigateUp() {
//This method is called when the up button is pressed. Just the pop back stack.
getSupportFragmentManager().popBackStack();
return true;
}

- 3,185
- 1
- 22
- 42
add to your onCreate in the top
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
and add a function to the activity
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
getSupportFragmentManager().popBackStack();
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
good luck

- 3,539
- 1
- 10
- 18
Add below line after setcontentview()
//by doin that Back arrow will appear
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Create following overridden method after onCreate().
@Override
public boolean onSupportNavigateUp() {
finish();
return super.onSupportNavigateUp();
}

- 198
- 1
- 8