I want to call a function in a fragment from a class. How do i attach FragmentActivity to External Interface like i attach Activities
Currently for Activity, the class contains
public interface externalInterface {
public void gotoNext();
}
public void setActivity(externalInterface activity) {
parentActivity = activity;
}
and i called
parentActivity.gotoNext();
Activity has
@Override
public void gotoNext() {
//Do something
}
Similarly i need to call gotoNext() function in a fragment from the same class.
If the function you want to call is already in the Activity then you can do something like the code below and it will work -
The key is, in the Fragment you have a method called getActivity()
which you can use to access methods in your activity.
public interface ExternalInterface {
public void gotoNext();
}
public MyActivity implments ExternalInterface {
public void gotoNext(){
//Do something here
}
}
In your Fragment, you can call
public MyFragment extends Fragment{
@Override
public void onCreateView(....){
((ExternalInterface )getActivity()).gotoNext();
}
}