标签:fragment fragment与activity通信
上一篇我们讲到与Fragment有关的常用函数,既然Fragment被称为是“小Activity”,现在我们来讲一下Fragment如何与Acitivity通信。如果上一篇还有不懂得,可以再看一下。传送门。
1.在动态添加Fragment的过程中,我们在Activity中通过Fragment.setArguments()的方法为Fragment提供数据;
2.在Fragment中,在onAttach()函数中通过调用getArguments()获得一个Bundle对象,从而获取我们提供的数据。
比如说:新闻浏览情境下,共有两个Fragment,一个是用来显示新闻标题;另外一个用来显示新闻内容。当我们点击新闻标题的时候,新闻的内容就显示到另外一个Fragment当中了。下面我们就来通过按下Fragment1中的button来传递一个消息到Fragment2中。
首先我们要做的是,在Fragment1中创建一个回调接口,并且在Activity中重写他的方法,传递信息给Fragment2(举一反三:我们也可以传递给其他的Fragment)。
下面我给出Fragment1的代码:
package com.example.fragment; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import com.example.fragmentdemo.R; public class Fragment1 extends Fragment { private static final String TAG = "FragmentDemo"; private OnButtonClickListener mListener; /** * 这里我们创建一个回调接口 * * @author Myp * */ public interface OnButtonClickListener { public void onButtonClicked(); } @Override public void onAttach(Activity activity) { Log.v(TAG, "Fragment1 onAttach"); Bundle args = getArguments(); if (null != args) { /* * 在这里我们可以将从Acivity收到的数据保存起来,并且显示到Fragment当中。 */ Log.d(TAG, "Fragment1 Get data from activity " + args.getString("hello")); } try { mListener = (OnButtonClickListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnButtonClickListener"); } super.onAttach(activity); } @Override public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) { Log.d(TAG, "onInflate"); super.onInflate(activity, attrs, savedInstanceState); } @Override public void onCreate(Bundle savedInstanceState) { Log.v(TAG, "Fragment1 onCreate"); super.onCreate(savedInstanceState); } @Override public void onDestroy() { Log.v(TAG, "Fragment1 onDestroy"); super.onDestroy(); } @Override public void onResume() { Log.v(TAG, "Fragment1 onResume"); super.onResume(); } @Override public void onStart() { Log.v(TAG, "Fragment1 onStart"); super.onStart(); } @Override public void onDetach() { Log.v(TAG, "Fragment1 onDetach"); super.onDetach(); } @Override public void onPause() { Log.v(TAG, "Fragment1 onPause"); super.onPause(); } @Override public void onStop() { Log.v(TAG, "Fragment1 onStop"); super.onStop(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.v(TAG, "Fragment1 onCreateView"); View view = inflater.inflate(R.layout.fragment1, container, false); // 为button注册事件,并且调用回调接口,将信息返回给上层 view.findViewById(R.id.fragment1_btn).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { mListener.onButtonClicked(); } }); return view; } @Override public void onDestroyView() { Log.v(TAG, "Fragment1 onDestroyView"); super.onDestroyView(); } @Override public void onActivityCreated(Bundle savedInstanceState) { Log.v(TAG, "Fragment1 onActivityCreated"); super.onActivityCreated(savedInstanceState); } }
1.创建回调接口;
2.为了确保宿主activity实现这个接口,fragment1的onAttach() 方法中通过将传入的activity强制类型转换,实例化一个OnButtonClickListener对象;
3.注册Button的Click事件,并在事件中调用回调方法。
然后我给出Activity的代码:
package com.example.fragmentdemo; import android.annotation.SuppressLint; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.widget.TextView; import com.example.fragment.Fragment1; import com.example.fragment.Fragment1.OnButtonClickListener; import com.example.fragment.Fragment2; @SuppressLint("Recycle") public class MainActivity extends FragmentActivity implements OnButtonClickListener { private static final String TAG = "FragmentDemo"; private Fragment1 fragment1; private Fragment2 fragment2; @Override protected void onCreate(Bundle savedInstanceState) { Log.i(TAG, "onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragment1 = new Fragment1(); fragment2 = new Fragment2(); addFragment1(); addFragment2(); } private void addFragment1() { Bundle args = new Bundle(); args.putString("hello", "Hello Fragment1"); fragment1.setArguments(args); Log.i(TAG, "create Fragment1"); FragmentTransaction transaction = getSupportFragmentManager() .beginTransaction(); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); transaction.add(R.id.one, fragment1); transaction.addToBackStack(null); transaction.commit(); } private void addFragment2() { Bundle args = new Bundle(); args.putString("hello", "Hello Fragment2"); fragment2.setArguments(args); Log.i(TAG, "create Fragment2"); FragmentTransaction transaction = getSupportFragmentManager() .beginTransaction(); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); transaction.replace(R.id.two, fragment2); transaction.addToBackStack(null); transaction.commit(); } @Override public void onAttachFragment(Fragment fragment) { Log.i(TAG, "onAttachFragment"); super.onAttachFragment(fragment); } @Override protected void onDestroy() { Log.i(TAG, "onDestroy"); super.onDestroy(); } @Override protected void onPause() { Log.i(TAG, "onPause"); super.onPause(); } @Override protected void onResume() { Log.i(TAG, "onResume"); super.onResume(); } @Override protected void onStart() { Log.i(TAG, "onStart"); super.onStart(); } @Override protected void onStop() { Log.i(TAG, "onStop"); super.onStop(); } @Override protected void onRestart() { Log.i(TAG, "onRestart"); super.onRestart(); } @Override public void onButtonClicked() { Log.i(TAG, "onButtonClicked"); TextView tv = (TextView) fragment1.getActivity().findViewById( R.id.fragment2_tv); tv.setText(tv.getText() + "\n从Fragment1收到数据!"); } }
1.通过implements实现回调接口;
2.重写回调方法,然后向Fragment2传递数据。
点我下载,下载源码导入工程就可以使用了!
【Android开发】之Fragment与Acitvity通信
标签:fragment fragment与activity通信
原文地址:http://blog.csdn.net/u010049692/article/details/38979607