Send data between fragments in a pagerAdapter

三世轮回 提交于 2019-12-02 04:37:13

Ok, as android guidelines says, you must create an interface in your fragment. For example:

public class armarFragment extends Fragment implements View.OnClickListener{
    ...
    private armarFragmentListener mListener;

    public interface armarFragmentListener {
        void onEnviar ();
    }

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (armarFragmentListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement armarFragmentListener ");
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btnEnviarALaCocina:
            //Do what you want.
            mListener.onEnviar(/*Change interface to pass arguments if you want*/);
        break;
    }
}

Your activity must implements that interface and do what corresponds, for example call the methods of the fragment to get the data. When you have the data you can pass it to the new fragment as arguments or otherwise.

When you create a blank fragment in android studio it put that structure on the class by default. Create a new blank fragment and take a look at it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!