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

后端 未结 6 1053
南笙
南笙 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:38

    1. Define an interface and call it IChangeListener (or something like that) and define a method inside which will do the work (ex, changeFragment()) (or call another method which will do the work) for changing the fragment).
    2. Make your activity implement the interface, or make an anonymous class within the activity implement it.
    3. Make a paramerized constructor of your fragment, which accepts a IChangeListener parameter.
    4. When initializing the fragment, pass your IChangeListener implementation (the anonymous class or the activity, implementing it)
    5. Make the onClick listener of the button call the changing method of the IChangeListener.

提交回复
热议问题