First create an interface
public interface MyInterface { void myAction() ; }
Your snippet should implement this interface.
public MyFragment extends Fragment implements MyInterface
In your activity, define a field of type MyInterface :
private MyInterface listener ; public void setListener(MyInterface listener) { this.listener = listener ; }
When creating a fragment and adding it:
setListener(myFragment);
Finally, when the condition arises that you want to call the Fragment method, simply call:
listener.myAction() ; // this will call the implementation in your MyFragment class.
Farhad faghihi
source share