First create an interface
public interface MyInterface
{
void myAction() ;
}
Your fragment must 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 your fragment and adding it :
setListener(myFragment);
Finally, when the condtion happens that you want to call the Fragment method, just call :
listener.myAction() ; // this will call the implementation in your MyFragment class.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…