簡體   English   中英

如何從 android 中的另一個片段調用片段方法

[英]How to call Fragment method from an another fragment in android

我想在 B class 片段中調用 A class 片段方法,這是最好的方法。 我已經看到了許多不同的方法,但每次應用程序崩潰

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

您可以使用 sharedViewModel。 就像如果您想在片段 B 中執行任何跟隨取消關注事件后更新片段 A 中的跟隨取消關注按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM