Calling a method in one fragment from another

前端 未结 5 1524
渐次进展
渐次进展 2020-12-18 00:30

I\'ve been trying for a while already (as well, searching in here) for something to help me to refresh listView in my MainFragment, when a button is pressed in other fragmen

5条回答
  •  渐次进展
    2020-12-18 01:17

    In MainFragment class you can do the following code:

    private static MainFragment instance = null;
    
    @Override  
    public void onCreate(@Nullable Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        instance = this;  
    }
    
    public static MainFragment getInstance() {  
        return instance;  
    } 
    

    And in SocialMedia class you can call the method as follows:

    MainFragment.getInstance().otherList();   
    

提交回复
热议问题