由于Fragment的生命周期完全依赖宿主Activity,所以当我们在使用Fragment时难免出现Activity和Fragment间的传值通信操作。
1、Activity向Fragment,通过声明的Fragment对象的setArguments(bundle)方法来实现Activity到Fragment的传递
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_communication); 5 6 FragmentManager fragmentMgr = getFragmentManager(); 7 FragmentTransaction transaction = fragmentMgr.beginTransaction(); 8 9 Fragment fragment = new CommunicationFragment(); 10 Bundle bundle = new Bundle(); 11 bundle.putCharSequence(MSG_KEY , MESSAGE); 12 fragment.setArguments(bundle); 13 14 transaction.replace(R.id.ll_communication, fragment); 15 transaction.commit(); 16 }
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
1 package com.baixd.app.framework.fragment; 2 3 import android.app.Activity; 4 import android.app.Fragment; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.TextView; 10 import android.widget.Toast; 11 12 import com.baixd.app.framework.R; 13 14 15 public class CommunicationFragment extends Fragment { 16 17 private View view; 18 19 private TextView mTextView; 20 21 private CharSequence msg; 22 23 private static final CharSequence RESP_MSG = "成功接收消息!"; 24 25 private OnFragmentInteractionListener mInteractionListener; 26 27 @Override 28 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 29 view = inflater.inflate(R.layout.fragment_communication, container, false); 30 31 Bundle bundle = this.getArguments(); 32 msg = bundle.getCharSequence(CommunicationActivity.MSG_KEY); 33 34 initView(); 35 36 Toast.makeText(getActivity(), "接收到了来自Fragment的消息:" + msg, Toast.LENGTH_SHORT).show(); 37 return view; 38 } 39 40 @Override 41 public void onAttach(Activity activity) { 42 mInteractionListener = (OnFragmentInteractionListener) activity; 43 mInteractionListener.onFragmentInteraction(RESP_MSG); 44 super.onAttach(activity); 45 } 46 47 private void initView() { 48 mTextView = (TextView) view.findViewById(R.id.tv_communiation_text); 49 mTextView.setText(msg); 50 } 51 52 public interface OnFragmentInteractionListener { 53 public void onFragmentInteraction(CharSequence c); 54 } 55 56 57 }
宿主Activity:CommunicationActivity.java
1 package com.baixd.app.framework.fragment; 2 3 import android.app.Fragment; 4 import android.app.FragmentManager; 5 import android.app.FragmentTransaction; 6 import android.support.v7.app.ActionBarActivity; 7 import android.os.Bundle; 8 import android.view.Menu; 9 import android.view.MenuItem; 10 import android.widget.Toast; 11 12 import com.baixd.app.framework.R; 13 14 public class CommunicationActivity extends ActionBarActivity implements CommunicationFragment.OnFragmentInteractionListener{ 15 16 public static final String MSG_KEY = "msg"; 17 18 public static final CharSequence MESSAGE = "向Fragment传递消息"; 19 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_communication); 25 26 FragmentManager fragmentMgr = getFragmentManager(); 27 FragmentTransaction transaction = fragmentMgr.beginTransaction(); 28 29 Fragment fragment = new CommunicationFragment(); 30 31 transaction.replace(R.id.ll_communication, fragment); 32 transaction.commit(); 33 } 34 35 @Override 36 public void onFragmentInteraction(CharSequence msg) { 37 Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 38 } 39 }