Skip to content Skip to sidebar Skip to footer

Pass Arraylist From Fragment To Its Own Activity

I have a fragments that builds a songs ArrayList, i want to pass that ArrayList to the framgment's activity . I know i can use interface, but not sure how i could do it public clas

Solution 1:

Change name of your interface so that it begins from capital letter. Then in your activity add this line to declaration of your activity: public class FragmentActivity extends Activity implements PassArrayList, override interface method. And in fragment add following:

PassArrayList mCallback;


@OverridepublicvoidonAttach(Activity activity) {
    super.onAttach(activity);
    mCallback = (PassArrayList) getActivity();
}

@OverridepublicvoidonDetach() {
    super.onDetach();
    mCallback = null;
}

And somewhere in your code, when you want to pass your list of songs back to the activity call the method onArticleSelected() on your mCallback object and pass to this method your arraylist. Then this arraylist will come as an argument to the method onArticleSelected() in your activity and you could make with it anything that you like. But don't forget to nullify link to mCallback in onDetach() hook method to prevent context leak

Post a Comment for "Pass Arraylist From Fragment To Its Own Activity"