从Avtivity往Fragment方向的通信的方法:
Activity ----> Fragment:
---- 在Activity中创建Bundle数据包, 并调用 Fragment 的 setArguments(Bundle bundle)方法, 然后在Fragment中接收该bundle数据包
有一点要注意的是:
---- 在Fragment中负责调用getArgument()进行接收数据的函数,一定要在Activity中的setArgument()之后调用, 否则将会接收不到信息。
下面直接上代码:
Activity中的代码:
public class MainActivity extends Activity {
EditText editText = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 从Activity中向Fragment中进行通信的方法 :
* Activity --> Fragment:
* 在Activity中创建Bundle数据包, 并调用 Fragment 的 setArguments(Bundle bundle)方法
* 然后在Fragment中接收该bundle数据包
*
* 有一点要注意的是:
* 在Fragment中负责调用getArgument()进行接收数据的函数,一定要在Activity中的setArgument()之后调用,
* 否则将会接收不到信息。
*/
editText = (EditText) findViewById(R.id.editText);
}
public void onClick(View view)
{
//向bundle中添加信息,然后调用setArguments(..)
String text = editText.getText().toString()+"";
MyFragment myFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("name", text);
myFragment.setArguments(bundle);//在这里发送Bundle
//将该 Fragment加载进Activity中,当绘制Fragment上的组件时,才会调用Fragment的onCreateView(..)函数
//因为获取getArgument()是写在onCreateView()中,所以才要先去加载Fragment进Activity中
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.add(R.id.container, myFragment, "fragment");
beginTransaction.commit();
}
}
Fragment中的代码:
public class MyFragment extends Fragment {
//每次创建时,绘制Fragment的组件时,调用该方法
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, null);
TextView tv = (TextView) view.findViewById(R.id.tv_fragment);
//在这里接收从Activity传递过来的Bundle中的数据信息
String str = getArguments().getString("name");
tv.setText("传送来的数据是 : " + str);
return view;
}
}
布局文件Activity_main.xml中的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xulunpeng.fragmenttongxin.MainActivity"
android:orientation="vertical">
<EditText
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:id="@+id/editText"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="将数据传送到Fragment上"
android:layout_gravity="center_horizontal"
android:onClick="onClick"/>
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:orientation="vertical"
android:id="@+id/container">
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@android:color/black"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是个Fragment"
android:textColor="@android:color/white"
android:layout_centerInParent="true"
android:textSize="20sp"
android:id="@+id/tv_fragment"/>
</RelativeLayout>
</RelativeLayout>
效果图:
点击传送数据前 和 传送了数据后的效果图:
总结 :
---- 就是在 Activity中通过 setArguments(..)传个 Bundle过去,然后在Fragment中调用getArguments()来接受该bundle