首页 > 移动平台 > 详细

Android开发:fragment将事件传递回activity

时间:2017-01-20 00:25:20      阅读:495      评论:0      收藏:0      [点我收藏+]

fragment触发事件后传递会给activity,可以通过在fragment中定义一个接口,让activity实现这个接口。

具体代码如下

public class AAFragment extends Fragment {
    private OnFragmentInteractionListener mListener;

   public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

   
    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
}

只要activity实现OnFragmentInteractionListener这个接口,在fragment中调用onFragmentInteraction,就能将事件传递给activity。

Android开发:fragment将事件传递回activity

原文:http://www.cnblogs.com/tootwo2/p/6309067.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!