由于Fragment的生命周期完全依赖宿主Activity,所以当我们在使用Fragment时难免出现Activity和Fragment间的传值通信操作。

1、Activity向Fragment,通过声明的Fragment对象的setArguments(bundle)方法来实现Activity到Fragment的传递

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_communication); FragmentManager fragmentMgr = getFragmentManager();
FragmentTransaction transaction = fragmentMgr.beginTransaction(); Fragment fragment = new CommunicationFragment();
Bundle bundle = new Bundle();
bundle.putCharSequence(MSG_KEY , MESSAGE);
fragment.setArguments(bundle); transaction.replace(R.id.ll_communication, fragment);
transaction.commit();
}

Fragment中获取Activity传递的数据,通过Bundle bundle = this.getArguments()方法获取数据

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_communication, container, false); Bundle bundle = this.getArguments();
msg = bundle.getCharSequence(CommunicationActivity.MSG_KEY); initView(); Toast.makeText(getActivity(), "接收到了来自Fragment的消息:" + msg, Toast.LENGTH_SHORT).show();
return view;
}

2、Fragment到Activity
在Fragment需要声明一个接口,定义回调方法,如CommunicationFragment中定义了OnFragmentInteractionListener内部接口,并声明mInteractionListener这个成员变量,当调用onAttach方法时将activity转换为OnFragmentInteractionListener,并调用它的方法传递数据。而在宿主Activity中需要实现OnFragmentInteractionListener接口,并实现方法public void onFragmentInteraction(CharSequence c),在方法中接收数据并处理

CommunicationFragment.java

 package com.baixd.app.framework.fragment;

 import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast; import com.baixd.app.framework.R; public class CommunicationFragment extends Fragment { private View view; private TextView mTextView; private CharSequence msg; private static final CharSequence RESP_MSG = "成功接收消息!"; private OnFragmentInteractionListener mInteractionListener; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_communication, container, false); Bundle bundle = this.getArguments();
msg = bundle.getCharSequence(CommunicationActivity.MSG_KEY); initView(); Toast.makeText(getActivity(), "接收到了来自Fragment的消息:" + msg, Toast.LENGTH_SHORT).show();
return view;
} @Override
public void onAttach(Activity activity) {
mInteractionListener = (OnFragmentInteractionListener) activity;
mInteractionListener.onFragmentInteraction(RESP_MSG);
super.onAttach(activity);
} private void initView() {
mTextView = (TextView) view.findViewById(R.id.tv_communiation_text);
mTextView.setText(msg);
} public interface OnFragmentInteractionListener {
public void onFragmentInteraction(CharSequence c);
} }

宿主Activity:CommunicationActivity.java

 package com.baixd.app.framework.fragment;

 import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast; import com.baixd.app.framework.R; public class CommunicationActivity extends ActionBarActivity implements CommunicationFragment.OnFragmentInteractionListener{ public static final String MSG_KEY = "msg"; public static final CharSequence MESSAGE = "向Fragment传递消息"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_communication); FragmentManager fragmentMgr = getFragmentManager();
FragmentTransaction transaction = fragmentMgr.beginTransaction(); Fragment fragment = new CommunicationFragment(); transaction.replace(R.id.ll_communication, fragment);
transaction.commit();
} @Override
public void onFragmentInteraction(CharSequence msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
05-06 22:26