1

I am having some issues with Fragment interactions..

I added four fragments on a particular Activity..

Code is the following:

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        SecAct_Foto_Fragment foto_fragment = new SecAct_Foto_Fragment();
        BUT_left_base_Fragment left_base_fragment = new BUT_left_base_Fragment();
        BUT_mid_base_Fragment mid_base_fragment = new BUT_mid_base_Fragment();
        BUT_right_base_Fragment right_base_fragment = new BUT_right_base_Fragment();

        getSupportFragmentManager().beginTransaction()
                .add(R.id.container_second, foto_fragment)
                .commit();

        getSupportFragmentManager().beginTransaction()
                .add(R.id.container_second, left_base_fragment)
                .add(R.id.container_second, mid_base_fragment)
                .add(R.id.container_second, right_base_fragment)
                .addToBackStack(null)
                .commit();
    }
}

Then I would like to remove all of these on button click in a fragment, each fragment with its own exit transition.

Problem is the fragments are not removed.. I have been reading through some stuff on the Fragment BackStack, but this seems to have nothing to do with it..

I cannot figure out why the fragments are not being removed?

public class BUT_right_base_Fragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.right_base_fragment, container, false);

        Button button = (Button)rootView.findViewById(R.id.right_base_button);
        final BUT_left_base_Fragment but_left_base_fragment = new BUT_left_base_Fragment();
        final BUT_mid_base_Fragment but_mid_base_fragment = new BUT_mid_base_Fragment();
        final BUT_left_FShape_Fragment but_left_fShape_fragment = new BUT_left_FShape_Fragment();
        final BUT_mid_FShape_Fragment but_mid_fShape_fragment = new BUT_mid_FShape_Fragment();
        final BUT_right_FShape_Fragment but_right_fShape_fragment = new BUT_right_FShape_Fragment();

        //the remove transactions do not function
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().beginTransaction()
                        .setCustomAnimations(R.anim.fade_in, R.anim.left_base_fshape)
                        .remove(but_left_base_fragment)
                        .setCustomAnimations(R.anim.fade_in, R.anim.mid_base_fshape)
                        .remove(but_mid_base_fragment)
                        .setCustomAnimations(R.anim.fade_in, R.anim.right_base_fshape)
                        .remove(but_right_base_fragment)
                        .addToBackStack(null)
                        .commit();

            }
        });

        return rootView;
    }
}
GianhTran
  • 3,443
  • 2
  • 22
  • 42
Niels Vanwingh
  • 604
  • 1
  • 6
  • 22

0 Answers0