Steps to make a Fragment or View to have a Callback to its parent Activity.
Step by step from info taken from here:
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
- We have an Activity that hosts a Fragment or a View
- We have a Fragment or View with a ListView or a Button or some element that we want the Activity to know about. i.e. we load detailed info of an element chosen on a ListView or something.
- You have to define a callback interface inside the Fragment or the view:
public static class FragmentA extends Fragment {
...
// Put this interface on the Fragment or the View
public interface OnItemClickedListener {
public void OnItemClicked(Parameters params);
}
...
}
- Main Activity has to implement the interface and override
OnItemClicked()
public class ParentActivity extends Activity implements FragmentA.OnItemClickedListener {
...
@Override
public void OnItemClicked(Parameters recieveparams) {
whateverYouWantDone();
}
...
}
- To ensure that the host activity implements the interface instantiate an
instance of
OnItemClicked()
cast the Activity passed intoonAttach()
if you use a callback in a custom View you can useonAttachedToWindow()
to perform the same:
public static class FragmentA extends Fragment {
OnItemClickedListener mListener;
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnItemClickedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnItemClickedListener");
}
}
...
}
on a View
public static class CustomView extends View {
OnItemClickedListener mListener;
...
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mListener = (OnItemClickedListener) this.getContext();
}
...
}
- The last thing you have to do is to create a
dummyCallback
implementation that does nothing but avoids a NullPointerException when the Fragment or View is not attached to an activity check also here for more explanation:
http://stackoverflow.com/questions/16956476/dummy-callback-interface
public static class FragmentA extends Fragment {
OnItemClickedListener mListener = sDummyCallbacks;
...
private static OnItemClickedListener sDummyCallbacks = new OnItemClickedListener() {
@Override
public void OnItemClicked(Parameters params) {
}
};
...
}
- And that's it. Now when you click on a button inside a Fragment or perform any action on a View that info can be received by the Activity who can perform any actions you want. Just let the button make the call: (Or the element on the ListView or the picture you draw on a Canvas... or whatever)
public static class FragmentA extends Fragment {
OnItemClickedListener mListener = sDummyCallbacks;
...
Button button = (Button) view.findViewById(R.id.imageButton1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//hello! anybody home?
mListener.OnItemClicked(Parameters params);
}
});
...
}
onAttach is deprecated onAttach(Activity activity)
but there is another onAttach