标签:des android style blog http color os io 使用
上一篇我们讲到与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的代码:
1 package com.example.fragment; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.support.v4.app.Fragment; 6 import android.util.AttributeSet; 7 import android.util.Log; 8 import android.view.LayoutInflater; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.view.ViewGroup; 12 13 import com.example.fragmentdemo.R; 14 15 public class Fragment1 extends Fragment { 16 17 private static final String TAG = "FragmentDemo"; 18 private OnButtonClickListener mListener; 19 20 /** 21 * 这里我们创建一个回调接口 22 * 23 * @author Myp 24 * 25 */ 26 public interface OnButtonClickListener { 27 public void onButtonClicked(); 28 } 29 30 @Override 31 public void onAttach(Activity activity) { 32 Log.v(TAG, "Fragment1 onAttach"); 33 Bundle args = getArguments(); 34 if (null != args) { 35 /* 36 * 在这里我们可以将从Acivity收到的数据保存起来,并且显示到Fragment当中。 37 */ 38 Log.d(TAG, 39 "Fragment1 Get data from activity " 40 + args.getString("hello")); 41 } 42 43 try { 44 mListener = (OnButtonClickListener) activity; 45 } catch (ClassCastException e) { 46 throw new ClassCastException(activity.toString() 47 + " must implement OnButtonClickListener"); 48 } 49 super.onAttach(activity); 50 } 51 52 @Override 53 public void onInflate(Activity activity, AttributeSet attrs, 54 Bundle savedInstanceState) { 55 Log.d(TAG, "onInflate"); 56 super.onInflate(activity, attrs, savedInstanceState); 57 } 58 59 @Override 60 public void onCreate(Bundle savedInstanceState) { 61 Log.v(TAG, "Fragment1 onCreate"); 62 super.onCreate(savedInstanceState); 63 } 64 65 @Override 66 public void onDestroy() { 67 Log.v(TAG, "Fragment1 onDestroy"); 68 super.onDestroy(); 69 } 70 71 @Override 72 public void onResume() { 73 Log.v(TAG, "Fragment1 onResume"); 74 super.onResume(); 75 } 76 77 @Override 78 public void onStart() { 79 Log.v(TAG, "Fragment1 onStart"); 80 super.onStart(); 81 } 82 83 @Override 84 public void onDetach() { 85 Log.v(TAG, "Fragment1 onDetach"); 86 super.onDetach(); 87 } 88 89 @Override 90 public void onPause() { 91 Log.v(TAG, "Fragment1 onPause"); 92 super.onPause(); 93 } 94 95 @Override 96 public void onStop() { 97 Log.v(TAG, "Fragment1 onStop"); 98 super.onStop(); 99 } 100 101 @Override 102 public View onCreateView(LayoutInflater inflater, ViewGroup container, 103 Bundle savedInstanceState) { 104 Log.v(TAG, "Fragment1 onCreateView"); 105 View view = inflater.inflate(R.layout.fragment1, container, false); 106 // 为button注册事件,并且调用回调接口,将信息返回给上层 107 view.findViewById(R.id.fragment1_btn).setOnClickListener( 108 new OnClickListener() { 109 @Override 110 public void onClick(View v) { 111 mListener.onButtonClicked(); 112 } 113 }); 114 return view; 115 } 116 117 @Override 118 public void onDestroyView() { 119 Log.v(TAG, "Fragment1 onDestroyView"); 120 super.onDestroyView(); 121 } 122 123 @Override 124 public void onActivityCreated(Bundle savedInstanceState) { 125 Log.v(TAG, "Fragment1 onActivityCreated"); 126 super.onActivityCreated(savedInstanceState); 127 } 128 129 }
1.创建回调接口;
2.为了确保宿主activity实现这个接口,fragment1的onAttach() 方法中通过将传入的activity强制类型转换,实例化一个OnButtonClickListener对象;
3.注册Button的Click事件,并在事件中调用回调方法。
然后我给出Activity的代码:
1 package com.example.fragmentdemo; 2 3 import android.annotation.SuppressLint; 4 import android.os.Bundle; 5 import android.support.v4.app.Fragment; 6 import android.support.v4.app.FragmentActivity; 7 import android.support.v4.app.FragmentTransaction; 8 import android.util.Log; 9 import android.widget.TextView; 10 11 import com.example.fragment.Fragment1; 12 import com.example.fragment.Fragment1.OnButtonClickListener; 13 import com.example.fragment.Fragment2; 14 15 @SuppressLint("Recycle") 16 public class MainActivity extends FragmentActivity implements 17 OnButtonClickListener { 18 19 private static final String TAG = "FragmentDemo"; 20 private Fragment1 fragment1; 21 private Fragment2 fragment2; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 Log.i(TAG, "onCreate"); 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 fragment1 = new Fragment1(); 29 fragment2 = new Fragment2(); 30 addFragment1(); 31 addFragment2(); 32 } 33 34 private void addFragment1() { 35 Bundle args = new Bundle(); 36 args.putString("hello", "Hello Fragment1"); 37 fragment1.setArguments(args); 38 Log.i(TAG, "create Fragment1"); 39 FragmentTransaction transaction = getSupportFragmentManager() 40 .beginTransaction(); 41 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 42 transaction.add(R.id.one, fragment1); 43 transaction.addToBackStack(null); 44 transaction.commit(); 45 } 46 47 private void addFragment2() { 48 Bundle args = new Bundle(); 49 args.putString("hello", "Hello Fragment2"); 50 fragment2.setArguments(args); 51 Log.i(TAG, "create Fragment2"); 52 FragmentTransaction transaction = getSupportFragmentManager() 53 .beginTransaction(); 54 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 55 transaction.replace(R.id.two, fragment2); 56 transaction.addToBackStack(null); 57 transaction.commit(); 58 } 59 60 @Override 61 public void onAttachFragment(Fragment fragment) { 62 Log.i(TAG, "onAttachFragment"); 63 super.onAttachFragment(fragment); 64 } 65 66 @Override 67 protected void onDestroy() { 68 Log.i(TAG, "onDestroy"); 69 super.onDestroy(); 70 } 71 72 @Override 73 protected void onPause() { 74 Log.i(TAG, "onPause"); 75 super.onPause(); 76 } 77 78 @Override 79 protected void onResume() { 80 Log.i(TAG, "onResume"); 81 super.onResume(); 82 } 83 84 @Override 85 protected void onStart() { 86 Log.i(TAG, "onStart"); 87 super.onStart(); 88 } 89 90 @Override 91 protected void onStop() { 92 Log.i(TAG, "onStop"); 93 super.onStop(); 94 } 95 96 @Override 97 protected void onRestart() { 98 Log.i(TAG, "onRestart"); 99 super.onRestart(); 100 } 101 102 @Override 103 public void onButtonClicked() { 104 Log.i(TAG, "onButtonClicked"); 105 TextView tv = (TextView) fragment1.getActivity().findViewById( 106 R.id.fragment2_tv); 107 tv.setText(tv.getText() + "\n从Fragment1收到数据!"); 108 } 109 }
1.通过implements实现回调接口;
2.重写回调方法,然后向Fragment2传递数据。
点我下载,下载源码导入工程就可以使用了!
【Android开发】之Fragment与Acitvity通信
标签:des android style blog http color os io 使用
原文地址:http://www.cnblogs.com/getherBlog/p/3949171.html