N
N
Neonoviiwolf2018-10-27 20:28:15
Java
Neonoviiwolf, 2018-10-27 20:28:15

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();
            }
        });

here I'm trying to delete it, maybe I'm not getting the link correctly, but when checking the logs, the tag gets the same
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;
    }
}

endured removal of a fragment in a class of the parent, a song.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question