2

I cannot get this working using most of the solutions here. Please check my code that starts the fragment on the activity.

public void selectFrag() {
    Fragment fr;

    fr = new SelectionFragment();

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    //I removed this since my animations give error with FrameLayout
    //fragmentTransaction.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_down);
    fragmentTransaction.replace(R.id.fragment_selection, fr);
    fragmentTransaction.commit();
}

On my fragment I have the following that is extending android.support.v4.app.Fragment;

public class SelectionFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    //Inflate the layout for this fragment
    View view = inflater.from(getActivity()).inflate(
            R.layout.fragment_attributes, container, false);

    //Close fragment using close icon
    ImageView close = (ImageView) view.findViewById(R.id.close_fragment);
    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            closefragment();
        }
    });

    return view;
}//End onCreateView Method

private void closefragment() {
    getActivity().getFragmentManager().popBackStack();
}

}

My XML contains ImageView inside a RelativeLayout

<ImageView
            android:id="@+id/close_fragment"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:paddingTop="3dp"
            android:paddingRight="3dp"
            android:src="@drawable/close_cross"/>

How can I set ImageView to listen to click that closes the fragment? I have only one fragment and nothing happens with my code above.

Sunday
  • 117
  • 2
  • 10
  • Possible duplicate of [How to close the current fragment by using Button like the back button?](http://stackoverflow.com/questions/20812922/how-to-close-the-current-fragment-by-using-button-like-the-back-button) – Taras Oct 10 '15 at 16:16
  • @Taras Check the code to close fragment I have used, it is exactly the same as the duplicate you linked. This is not working with my ImageView. – Sunday Oct 10 '15 at 16:22
  • But the code to ADD fragment is different. Pay attention to _use addToBackstack() before commit()_ part of accepted answer. – Taras Oct 10 '15 at 16:23

4 Answers4

4

Remove getActivity() from closefragment()

private void closefragment() {
    getFragmentManager().popBackStack();
}
Zack
  • 1,527
  • 2
  • 20
  • 32
1

Solution is following:

fragmentTransaction.replace(R.id.fragment_selection, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();`
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Neil
  • 664
  • 6
  • 16
  • Sorry I had tried this code before but I posted the code while I was testing without addToBackStack(). It still does not close the fragment. – Sunday Oct 10 '15 at 16:35
  • This line:`getActivity().getFragmentManager().popBackStack();`, shouldn't it be `getSupportFragmentManager` ? – Neil Oct 10 '15 at 16:38
  • Or you can just call `getFragmentManager().popBackStack()` without `getActivity()` – Neil Oct 10 '15 at 16:39
  • I am using import android.support.v4.app.FragmentManager; hence I call getSupportFragmentManager() – Sunday Oct 10 '15 at 16:46
0

popBackStack(); only reverses your previous transaction. If you want to close this Fragment you should call

getActivity().getFragmentManager().beginTransaction().remove(this).commit();
Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55
0

Try this pls.

private void closefragment() {
getParentFragment().getFragmentManager().popBackStack(); }

or this

private void closefragment() {
getActivity.finish(); }
Alex
  • 1
  • 1