Call an activity method from a fragment

前端 未结 14 661
广开言路
广开言路 2020-11-22 09:08

Trying to call a method in my activity from a fragment. I want the fragment to give the method data and to get the data when the method return. I want to achieve similar to

相关标签:
14条回答
  • 2020-11-22 09:41

    I have been looking for the best way to do that since not every method we want to call is located in Fragment with same Activity Parent.

    In your Fragment

    public void methodExemple(View view){
    
            // your code here
    
            Toast.makeText(view.getContext(), "Clicked clicked",Toast.LENGTH_LONG).show();
        }
    

    In your Activity

    new ExempleFragment().methodExemple(context); 
    
    0 讨论(0)
  • 2020-11-22 09:46

    Thanks @BIJAY_JHA and @Manaus. I used the Kotlin version to call my signIn() method that lives in the Activity and that I'm calling from a Fragment. I'm using Navigation Architecture in Android so the Listener interface pattern isn't in the Fragment:

     (activity as MainActivity).signIn() 
    
    0 讨论(0)
提交回复
热议问题