How to add "UP" back button in fragment (Fragment to Activity)
Asked Answered
S

6

15

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?

Smacker answered 27/7, 2017 at 8:59 Comment(1)
what did you do to create it?Schizogenesis
V
24

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();
        }
    });
Varix answered 27/7, 2017 at 9:1 Comment(14)
Inside your layout file.Varix
error shown : app:layout_collapseMode="pin" app:navigationIcon="?attr/homeAsUpIndicatorSmacker
Remove that collapseModeVarix
but still shown error in (app:navigationIcon="?attr/homeAsUpIndicator") (app is red colour)Smacker
xmlns:app="schemas.android.com/apk/res-auto" Use this in your parent layout.Varix
You can also use: alt plus enter keyVarix
yes now fix, but where put this code, in fragment (Toolbar toolbar = (ToolBar) getActivity().findViewById(R.id.toolbarId); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onBackPressed(); } });)Smacker
Inside onViewCreated.Varix
after put this code on onViewCreated toolbar id is not shown & (ToolBar), onBackPressed(); red in colour...Smacker
you can implement your own instead of onBackPress method. Like Intent to move from one to another. you haven't said what to do by that back button. you have to import toolbar: import android.support.v7.widget.Toolbar; Varix
sir if you provide email id then I send my code please help meSmacker
There is back icon or not in the view.Varix
You are not using mine suggestion?Varix
Sir this is my project file - drive.google.com/open?id=0B8YTgurIRbm5R0ozVjFINFVERW8 (I want to toobar back function)Smacker
C
7

Call this method in your fragment onCreateView

public void showBackButton() {
if (getActivity() instanceof ActionBarActivity) {
    ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
Chinch answered 27/7, 2017 at 9:3 Comment(4)
this is the best answerInglis
inside the fragment onCreateViewChinch
after put this code (return v;) v is red, I am put this code after (return v;) but still shown red what to doSmacker
this shows thee toolbar, but it doesn't navigate back to the previous fragment..Chrysoprase
O
4

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();
        }
        });
    
Outhouse answered 23/9, 2018 at 7:53 Comment(0)
R
1

It might help in case if you want to come back from fragment to previous location.

Put this in your class with navController.

  @Override
public boolean onSupportNavigateUp() {
    navController.navigateUp();
    return super.onSupportNavigateUp();
}

don't forget implement relevant dependency such as navigation and navigation UI.

Radiate answered 8/4, 2022 at 12:46 Comment(0)
P
0

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>
Pygmy answered 27/7, 2017 at 9:17 Comment(2)
I asked you to add this code in xml after that check in design part. no need any fragment code for thisPygmy
Once the back button is visible make intent call to it to go from fragment to activityPygmy
F
0

You can easily do that, if you are using a Custom back button that is placed on your Custom top app bar, in the button's onClick() function you can call.. getActivity().onBackPressed(); it would work the same as if you have clicked the android navigation's back button...

Flippant answered 19/8, 2020 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.