0

I have a activity_settings.xml file like below in this file I have 3 text views if I click one it will take me to a fragment

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.crowderia.chat.SettingsActivity"
    android:background="@color/black">

    <TextView
        android:id="@+id/tv_added_me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Added Me" />

    <TextView
        android:id="@+id/tv_add_friends"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Friends" />

    <TextView
        android:id="@+id/tv_my_friends"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My Friends" />

    <FrameLayout
        android:id="@+id/settings_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

</RelativeLayout>

My SettingsActivity.class like below

onclick event for each text view

tv_added_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragment(new AddedMeFragment());
            }
        });

set fragment method

public void setFragment(Fragment f) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
        fragmentTransaction.replace(R.id.settings_fragment, f);
        fragmentTransaction.commit();
    }

If I click each text view it will take me to each fragment but after I click back its not going to go to Settings Activity how can I make this work

Please help me Im new to these stuff

HemalHerath
  • 1,006
  • 2
  • 18
  • 38

1 Answers1

1

You should remove all the fragments from FragmentManager on the click of back button.

FragmentManager fm = getSupportFragmentManager();
for (Fragment f : fm.getFragments()) {
    fm.beginTransaction().remove(f).commit();
}
Paresh P.
  • 6,677
  • 1
  • 14
  • 26
  • where should i put this one can u explain a little please – HemalHerath Nov 18 '17 at 04:37
  • 1
    in @override onBackPressed() method. – Sarthak Gandhi Nov 18 '17 at 04:40
  • If you are willing to perform on device back button, you shall write into `onBackPressed()`. If you are using toolbar with back arrow, you should write in the click of back arrow. – Paresh P. Nov 18 '17 at 04:41
  • @Override public void onBackPressed() { super.onBackPressed(); FragmentManager fm = getSupportFragmentManager(); for (Fragment f : fm.getFragments()) { fm.beginTransaction().remove(f).commit(); } } something like this right – HemalHerath Nov 18 '17 at 05:53
  • Remove `super.onBackPressed()` because it will close your `activity` – Paresh P. Nov 18 '17 at 06:10