Answer the question
In order to leave comments, you need to log in
I can't get a link to a nested fragment, where is the error?
Good.
In my fragment, more fragments are dynamically added, I need the unnecessary ones to be deleted by pressing the button. app crashes with nullpoint, what am I doing wrong?
I'm creating a fragment on click. GenerateId.generateViewId() - gives a unique number, I use it as a tag
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CreateMenuItemMenu itemMenu = new CreateMenuItemMenu();
String tag = String.valueOf(GenerateId.generateViewId());
getChildFragmentManager().beginTransaction()
.add(R.id.create_menu_card_view_container, itemMenu, tag)
.commit();
}
});
public class CreateMenuItemMenu extends Fragment {
private View rootView;
private FragmentManager fm;
private FragmentTransaction ft;
@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
AppCompatImageButton rootView = inflater.inflate(R.layout.create_menu_item_menu, container, false);
buttonClose = rootView.findViewById(R.id.create_menu_item_menu_image_button_close);
buttonClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fm = getChildFragmentManager();
Fragment fr = fm.findFragmentByTag(CreateMenuItemMenu.this.getTag());
ft = fm.beginTransaction();
ft.remove(fr);
ft.commit();
}
});
return rootView;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question