[英]How to call Fragment method from an another fragment in android
I want to call A class fragment method in B class Fragment what is the best approach.我想在 B class 片段中调用 A class 片段方法,这是最好的方法。 I already saw many different approach but every time app getting crash我已经看到了许多不同的方法,但每次应用程序崩溃
public class **AFragment** extends Fragment {
static AFragment instance=null;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
}
public void followUnfollowUser() {
//My main code is here
}
public static AFragment getInstance() {
return instance;
}
public class **BFragment** extends Fragment {
public void call_A_method() {
AFragment.getInstance().followUnfollowUser();
}
you can use sharedViewModel.您可以使用 sharedViewModel。 like if you want to update the followunfollow button in fragment A after doing any followunfollow event in Fragment B.就像如果您想在片段 B 中执行任何跟随取消关注事件后更新片段 A 中的跟随取消关注按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.