Calling a method in one fragment from another

前端 未结 5 1486
渐次进展
渐次进展 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:09

    you should use interface for this. define an interface in OtherFragment class and implement that in your MainActivity and define a public method in your MainFragment for refreshing your ListView and call that method from your MainActivity. here is an example :

    public Class OtherFragment extends Fragment implements OnClickListener {
    
    private Communicator communicator;
    
       ...
    
    public void setCommunicator(Communicator communicator) {
        this.communicator = communicator;
    }
    
    @Override
    public void onClick(View v) {
       communicator.clicked();
    }
    
       public interface Communicator {
          public void clicked();
       }
    }
    

    and in your MainActivity :

    public class MainActivity extends Activity implements OtherFragment.Communicator {
    
       MainFragment mainFragment;
    
       @Override
       protected void onCreate(Bundle b) {
          ...
          OtherFragment otherFragment = new OtherFragment();
          otherFragment.setCommunicator(this);
          ...
       }
    
       ...
    
       @Override 
       public void clicked() {
         mainFragment.updateList();
       }
    }
    

    and in your MainFragment :

    public class MainFragment extends Fragment {
    
        ...
    
        public void updateList() {
            // update list
        }
    }
    
    0 讨论(0)
  • 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();   
    
    0 讨论(0)
  • 2020-12-18 01:29
    FragmentManager fm = getFragmentManager(); 
    MainFragment fragm = (MainFragment)fm.findFragmentById(R.id.main_fragment); 
    fragm.otherList(); 
    

    This code worked best for me. And seems quite easy

    0 讨论(0)
  • 2020-12-18 01:32

    To call a method from another object (a Fragment is an object) you need to have the reference of that object. So if you want to call MainFragment#otherList() from a SocialMedia instance, SocialMedia needs a reference to MainFragment.

    For example:

    public class SocialMedia {
    
        private MainFragment mainFragment = null;
    
        public void setMainFragment(final MainFragment mainFragment) {
            this.mainFragment = mainFragment
        }
    }
    

    Then you can use this.mainFragment.otherList().

    0 讨论(0)
  • 2020-12-18 01:32
                // Calling Parent Fragment method from Child Fragment.
            // Child Fragment
            AppCompatActivity activity = (AppCompatActivity) view.getContext();
            FragmentManager fragmentManager = activity.getSupportFragmentManager();
            NavigateFragment navigateFragment = (NavigateFragment) fragmentManager.findFragmentById(R.id.fragment_place_dashboard);
            navigateFragment.sayHelloWorld();
    
    
            // -- Some method in Parent Fragment(NavigateFragment).
            public void sayHelloWorld(){
                Log.d(TAG, "Hello World.");
            }
    
    0 讨论(0)
提交回复
热议问题