How to replace a Fragment on button click of that fragment?

后端 未结 6 1045
南笙
南笙 2020-11-30 09:00

I have an activity containing multiple fragments. Activity initially have fragment and in it have two buttons. Upon clicking this button I have to replace the fragment by ne

6条回答
  •  既然无缘
    2020-11-30 09:42

    you can try this code it's work fine with me , inflate the layout to view , Define the buton and on click ,

    Button btn_unstable;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home,container,false);
    
        btn_unstable = (Button)view.findViewById(R.id.btn_unstable);
        btn_unstable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
        getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_replace, new UnstableFragment()).commit();
    
            }
        });
        return view;
    }
    

提交回复
热议问题