У меня возникли проблемы с взаимодействием фрагментов.
Я добавил четыре фрагмента в конкретное задание ..
Код следующий:
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();
}
}
Затем я хотел бы удалить все эти при нажатии кнопки во фрагменте, каждый фрагмент имеет свой собственный выходной переход.
Проблема в том, что фрагменты не удалены. Я читал некоторые вещи в BackStack фрагмента, но это, похоже, не имеет к этому никакого отношения.
Я не могу понять, почему фрагменты не удаляются?
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;
}
}