fragment给fragment传值

数据源fragment:

SPMainActivity activity=(SPMainActivity)getActivity();
activity.fragTOFragsendMessage(search);//调用activity的方法,参数是传递的值。


activity:

public void fragTOFragsendMessage(String search){
   Message msg = new Message();
   Bundle bundle = new Bundle();
   bundle.putString("search", search);
   msg.obj = bundle;
   msg.what = 111;
   mHandler.sendMessage(msg);
}
public void setHandler(Handler handler) {
   mHandler = handler;
}

接收数据的fragment:

private SPMainActivity mActivity;
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mActivity = (SPMainActivity) activity;
    mActivity.setHandler(mHandler);
}
private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 111:
                Bundle bundle=(Bundle)(msg.obj);
                search=bundle.getString("search");
                refreshData();
                break;
            default:
                break;
        }
    }
};
mActivity = (SPMainActivity) activity ; mActivity.setHandler( mHandler) ;}

你可能感兴趣的:(android)