Home » Android » android – How to add "UP" back button in fragment (Fragment to Activity)

android – How to add "UP" back button in fragment (Fragment to Activity)

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to go from fragment to activity using back button using toolbar back icon.

like this one

The fragment is my navigation drawer item & activity is my MainActivity.

How do I do it?

How to&Answers:

You can use app:navigationIcon="?attr/homeAsUpIndicator" for that back navigation icon.

 <android.support.v7.widget.Toolbar
            android:id="@+id/toolbarId"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:navigationIcon="?attr/homeAsUpIndicator"/>

For navigation:

Toolbar toolbar = (ToolBar) getActivity().findViewById(R.id.toolbarId);

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getActivity().onBackPressed();
        }
    });

Answer:

Try this worked for me :

  1. in XML:

    <android.support.v7.widget.Toolbar
     android:id="@+id/profileToolbar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
    
  2. Create an back arrow icon in drawable folder. Name it ‘ic_back_button’.
    Not sure how :-
    just right click on drawable > new > ImageAsset > Clip Art > Search back > select > OK > Finish (don’t forget to change the name).

  3. then Inside your fragment in onCreateView :

        Toolbar toolbar = view.findViewById(R.id.profileToolbar);
        toolbar.setNavigationIcon(R.drawable.ic_back_button);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          getActivity().onBackPressed();
        }
        });
    

Answer:

Call this method in your fragment onCreateView

public void showBackButton() {
if (getActivity() instanceof ActionBarActivity) {
    ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}

Answer:

Add this xml code to your fragment and try

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#FFFFFF"
        android:layout_gravity="center"
        android:gravity="center_horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title Here"
            android:typeface="serif"
            android:layout_gravity="center"
            android:id="@+id/toolbar_title"
            android:textSize="20sp"
            android:textColor="@android:color/black"/>

        <ImageView
            android:id="@+id/ivback_water"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:padding="15dp"
            android:scaleType="fitCenter"
            android:layout_gravity="left"
            android:background="@drawable/ic_arrow_back_black_24dp" />


    </android.support.v7.widget.Toolbar>