
前言
-
Activity與Fragment的使用在Android開發(fā)中非常多 - 今天,我將主要講解
Activity與Fragment如何進(jìn)行通信,實(shí)際上是要解決兩個(gè)問(wèn)題:-
Activity如何傳遞數(shù)據(jù)到Fragment? -
Fragment如何傳遞數(shù)據(jù)到Activity?
-
下面,我將解答這兩個(gè)問(wèn)題。
閱讀本文前,建議閱讀Android:Fragment最全面介紹 & 使用方法解析
問(wèn)題1: Activity 如何傳遞數(shù)據(jù)到 Fragment?
答:采用 Bundle方式。具體Demo步驟如下:
- 步驟1:
Activity的布局文件
activcity_2_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="我是Activity" />
<FrameLayout
android:layout_below="@+id/button"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="500dp"/>
</LinearLayout>
- 步驟2:設(shè)置
Fragment的布局文件
fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
>
<TextView
android:id="@+id/fragment"
android:text="我是fragment"
android:layout_gravity="center"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/text"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="等待Activity發(fā)送消息" />
<Button
android:id="@+id/button"
android:layout_gravity="center"
android:text="點(diǎn)擊接收Activity消息"
android:layout_centerInParent="true"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
- 步驟3:設(shè)置
Activity的類文件
Activity2Fragment
public class Activity2Fragment extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activcity_2_fragment);
text = (TextView) findViewById(R.id.text);
// 步驟1:獲取FragmentManager
FragmentManager fragmentManager = getFragmentManager();
// 步驟2:獲取FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 步驟3:創(chuàng)建需要添加的Fragment
final mFragment fragment = new mFragment();
// 步驟4:創(chuàng)建Bundle對(duì)象
// 作用:存儲(chǔ)數(shù)據(jù),并傳遞到Fragment中
Bundle bundle = new Bundle();
// 步驟5:往bundle中添加數(shù)據(jù)
bundle.putString("message", "I love Google");
// 步驟6:把數(shù)據(jù)設(shè)置到Fragment中
fragment.setArguments(bundle);
// 步驟7:動(dòng)態(tài)添加fragment
// 即將創(chuàng)建的fragment添加到Activity布局文件中定義的占位符中(FrameLayout)
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
}
- 步驟4:設(shè)置
Fragment的類文件
mFragment.java
public class mFragment extends Fragment {
Button button;
TextView text;
Bundle bundle;
String message;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View contentView = inflater.inflate(R.layout.fragment, container, false);
// 設(shè)置布局文件
button = (Button) contentView.findViewById(R.id.button);
text = (TextView) contentView.findViewById(R.id.text);
// 步驟1:通過(guò)getArgments()獲取從Activity傳過(guò)來(lái)的全部值
bundle = this.getArguments();
// 步驟2:獲取某一值
message = bundle.getString("message");
// 步驟3:設(shè)置按鈕,將設(shè)置的值顯示出來(lái)
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 顯示傳遞過(guò)來(lái)的值
text.setText(message);
}
});
return contentView;
}
}
展示結(jié)果

示意圖
至此,Activity 傳遞數(shù)據(jù)到 Fragment 講解完畢。
問(wèn)題2:Fragment 如何傳遞數(shù)據(jù)到 Activity
- 答:采用 接口回調(diào) 方式。
- 接口回調(diào) 回顧
把實(shí)現(xiàn)了某一接口的類所創(chuàng)建的對(duì)象的引用 賦給 該接口聲明的變量,通過(guò)該接口變量 調(diào)用 該實(shí)現(xiàn)類對(duì)象的實(shí)現(xiàn)的接口方法。
// 接口聲明的變量
Com com;
// 實(shí)現(xiàn)了Com接口的類(Com1)所創(chuàng)建的對(duì)象的引用 賦給 該接口聲明的變量
Com com = new Com1;
// 通過(guò)該接口變量(com) 調(diào)用 該實(shí)現(xiàn)類對(duì)象(Com1)的實(shí)現(xiàn)的接口方法(carson())
com.carson();
具體Demo
- 步驟1:在
Activity的布局文件定義1占位符(FrameLayout)
activity_main.xml
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="scut.carson_ho.fragment_2_activity.MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="等待Fragment發(fā)送消息" />
<Button
android:id="@+id/button"
android:layout_below="@+id/text"
android:text="點(diǎn)擊接收Fragment消息"
android:layout_centerInParent="true"
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_below="@+id/button"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="500dp"/>
</RelativeLayout>
- 步驟2:設(shè)置
Fragment的布局文件
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/fragment"
android:text="我是fragment"
android:gravity="center"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"/>
</LinearLayout>
- 步驟3:設(shè)置回調(diào)接口
該接口用于用于Activity與Fragment通信
ICallBack.java
public interface ICallBack {
void get_message_from_Fragment(String string);
}
- 步驟4:設(shè)置
Fragment的類文件
mFragment.java
public class mFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View contentView = inflater.inflate(R.layout.fragment, container, false);
// 設(shè)置布局文件
return contentView;
}
// 設(shè)置 接口回調(diào) 方法
public void sendMessage(ICallBack callBack){
callBack.get_message_from_Fragment("消息:我來(lái)自Fragment");
}
}
- 步驟5:設(shè)置
Acticvity的類文件
Main_Activity.java
public class MainActivity extends AppCompatActivity {
Button button;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
text = (TextView)findViewById(R.id.text);
// 步驟1:獲取FragmentManager
FragmentManager fragmentManager = getFragmentManager();
// 步驟2:獲取FragmentTransaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 步驟3:創(chuàng)建需要添加的Fragment
final mFragment fragment = new mFragment();
// 步驟4:動(dòng)態(tài)添加fragment
// 即將創(chuàng)建的fragment添加到Activity布局文件中定義的占位符中(FrameLayout)
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 通過(guò)接口回調(diào)將消息從fragment發(fā)送到Activity
fragment.sendMessage(new ICallBack() {
@Override
public void get_message_from_Fragment(String string) {
text.setText(string);
}
});
}
});
}
}
結(jié)果展示

示意圖.gif
至此,將數(shù)據(jù)從 Fragment 發(fā)送到 Activity 講解完畢
總結(jié)
- 看完本文,你應(yīng)該非常清楚該如何實(shí)現(xiàn)
Activity與Fragment相互通信

示意圖
- Carson帶你學(xué)四大組件文章系列:
Carson帶你學(xué)Android:頁(yè)面活動(dòng)-Activity
Carson帶你學(xué)Android:廣播-BroadcastReceiver
Carson帶你學(xué)Android:服務(wù)-Service
Carson帶你學(xué)Android:內(nèi)存承載器-ContentProvider
歡迎關(guān)注Carson_Ho的簡(jiǎn)書
不定期分享關(guān)于安卓開發(fā)的干貨,追求短、平、快,但卻不缺深度。
