Skip to content Skip to sidebar Skip to footer

Action Bar With Back Arrow

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

Solution 1:

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.

Solution 2:

I have some thing like this for back button in toolbar instead of action bar.

In activity_main.xml :

<RelativeLayoutxmlns: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.Toolbarandroid:id="@+id/toolbarId"android:layout_width="match_parent"android:layout_height="48dp"android:background="@color/colorPrimary"><TextViewandroid: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><ImageButtonandroid: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:

<vectorxmlns:android="http://schemas.android.com/apk/res/android"android:width="24dp"android:height="24dp"android:viewportWidth="24.0"android:viewportHeight="24.0"><pathandroid: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.

Solution 3:

Try below code

Implement OnBackStackChangedListener and add this code to your Fragment Activity.

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    //Listen for changes in the back stackgetSupportFragmentManager().addOnBackStackChangedListener(this);
    //Handle when activity is recreated like on orientation ChangeshouldDisplayHomeUp();
}

@OverridepublicvoidonBackStackChanged() {
    shouldDisplayHomeUp();
}

publicvoidshouldDisplayHomeUp(){
   //Enable Up button only  if there are entries in the back stackboolean canback = getSupportFragmentManager().getBackStackEntryCount()>0;
   getSupportActionBar().setDisplayHomeAsUpEnabled(canback);
}

@OverridepublicbooleanonSupportNavigateUp() {
    //This method is called when the up button is pressed. Just the pop back stack.getSupportFragmentManager().popBackStack();
    returntrue;
}

Solution 4:

add to your onCreate in the top

ActionBaractionBar= getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

and add a function to the activity

publicbooleanonOptionsItemSelected(MenuItem item) {
    intid= item.getItemId();
    if (id == android.R.id.home) {
        getSupportFragmentManager().popBackStack();
        finish();
        returntrue;
    }
    returnsuper.onOptionsItemSelected(item);
}

good luck

Solution 5:

Add below line after setcontentview()

//by doin that Back arrow will appeargetSupportActionBar().setDisplayHomeAsUpEnabled(true);

Create following overridden method after onCreate().

@OverridepublicbooleanonSupportNavigateUp() {
        finish();
        returnsuper.onSupportNavigateUp();
    }

Post a Comment for "Action Bar With Back Arrow"