android Fragment與Activity互動,互相發資料(附圖詳解)
筆者近期看官方training,發現了很多有用又好玩的知識。其中,fragment與Activity通訊就是一個。
fragment與Activity通訊主要是兩點:
1、fragment傳遞資訊給Activity
此點是通過在fragment中定義介面與Activity共享資料。
2、Activity傳遞資訊給fragment
此點主要是通過fragment的getArgument()和setArgument()兩個函式傳遞bundle來傳遞。
效果:(最後附上原始碼)
主要流程:
1、在MainActivity中設定一個fragment容器,在MainActivity初始化的時候替換成OneFragment。
2、OneFragment中由於定義了介面,在MainActivity中實現了此介面,當點選button的時候會實現此介面中的函式,並且和Activity共享此資料。(也就是EditText中的內容)
3、MainActivity中獲取到資料後,建立一個新的TwoFragment,並且使用Fragment的setArgument()方法把獲取到的資料傳遞到TwoFragment,TwoFragment使用getArgument()接收。
程式碼:
MainActivity:
package com.example.trainingfragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends Activity
implements OneFragment.OnOneFragmentClickListener{
private FragmentManager fm;
private FragmentTransaction ft;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fm=getFragmentManager();
ft=fm.beginTransaction();
ft.replace(R.id.fragment_container,new OneFragment());
//使用FragmentTransaction一定不要忘記提交
ft.commit();
}
@Override
public void onArticleClick(String text) {
//此處定義一個新的TwoFragment
//並且把在OneFragment獲取到的text傳入TwoFragment
TwoFragment newFragment = new TwoFragment();
Bundle args=new Bundle();
args.putString("text", text);
newFragment.setArguments(args);
//由於之前的一個FragmentTransaction已經提交過,所以此處需要重新定義
ft=fm.beginTransaction();
ft.replace(R.id.fragment_container, newFragment);
//使用FragmentTransaction一定不要忘記提交
ft.commit();
}
}
OneFragment:
package com.example.trainingfragment;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
/**
* 專案名稱:TrainingFragment
* 類描述:
* 建立人:佳佳
* 建立時間:2016/3/25 17:57
* 修改人:佳佳
* 修改時間:2016/3/25 17:57
* 修改備註:
*/
public class OneFragment extends Fragment {
OnOneFragmentClickListener mCallback;
private EditText et;
private Button btn;
//此處定義介面
public interface OnOneFragmentClickListener {
//在介面中定義函式
void onArticleClick(String text);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one,container,false);
et=(EditText)view.findViewById(R.id.et_one);
btn=(Button)view.findViewById(R.id.btn_one);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCallback.onArticleClick(et.getText().toString());
}
});
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnOneFragmentClickListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
}
TwoFragment:
package com.example.trainingfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* 專案名稱:TrainingFragment
* 類描述:
* 建立人:佳佳
* 建立時間:2016/3/25 17:57
* 修改人:佳佳
* 修改時間:2016/3/25 17:57
* 修改備註:
*/
public class TwoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
@Override
public void onStart() {
super.onStart();
//獲取Activity傳遞過來的資料並且顯示到
Bundle args=getArguments();
setMyText(args.getString("text"));
}
public void setMyText(String text) {
TextView tv=(TextView)getActivity().findViewById(R.id.tv_two);
tv.setText(text);
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_one:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<EditText
android:id="@+id/et_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入要傳遞的內容" />
<Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳轉並傳遞"
android:textSize="20sp" />
</LinearLayout>
fragment_two:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<EditText
android:id="@+id/et_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入要傳遞的內容" />
<Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳轉並傳遞"
android:textSize="20sp" />
</LinearLayout>
相關文章
- Activity和fragment是如何互動的Fragment
- 3-AVI–Activity與Fragment的資料傳遞Fragment
- Android--單Activity+多Fragment,玩轉FragmentAndroidFragment
- Android與WebView資料互動AndroidWebView
- Android | Activity和Fragment最全生命週期+發現大牛AndroidFragment
- Flutter Android 端 Activity/Fragment 流程原始碼分析FlutterAndroidFragment原始碼
- Android FlatBuffers資料互動Android
- Android ReactNative資料互動AndroidReact
- Android技術棧(一)從Activity遷移到FragmentAndroidFragment
- Android 與 JavaScript 互動 支援的資料型別AndroidJavaScript資料型別
- Activity、Fragment和IntentFragmentIntent
- Android之Activity啟動流程詳解(基於api28)AndroidAPI
- Android 整合 Flutter 及通訊互動詳解AndroidFlutter
- Android程式啟動與Activity顯示Android
- Fragment傳值到ActivityFragment
- Android與Flutter混合開發-UI互動AndroidFlutterUI
- Android 碎片(Fragment)講解AndroidFragment
- 將一個Activity中的資料傳到另一個Activity的Fragment中的方法Fragment
- 詳解Android中的四大元件之一:Activity詳解Android元件
- 前後端資料互動(四)——fetch 請求詳解後端
- Flutter 與 Android 的互動FlutterAndroid
- WebView詳解與簡單實現Android與H5互調WebViewAndroidH5
- 圖解大資料 | 海量資料庫查詢-Hive與HBase詳解圖解大資料資料庫Hive
- Python|Python互動之mongoDB互動詳解PythonMongoDB
- Android 向量圖詳解Android
- 前後端資料互動(二)——原生 ajax 請求詳解後端
- 前後端資料互動(一)——網路請求詳解後端
- Android webview 與 js(Vue) 互動AndroidWebViewJSVue
- Hive 與 ElasticSearch 的資料互動HiveElasticsearch
- python與mysql資料庫互動PythonMySql資料庫
- Android ViewPager2 + Fragment 聯動AndroidViewpagerFragment
- ActivityRecord、TaskRecord、ActivityStack以及Activity啟動模式詳解模式
- 與低層次互動活動相比,深層次互動活動的參與頻率更高(附原資料表)
- Vue元件之間的資料傳遞(通訊、互動)詳解Vue元件
- Android:動畫詳解Android動畫
- Activity和Fragment有什麼區別Fragment
- 詳解資料驅動
- React Native與Android通訊互動React NativeAndroid
- PHP與Python進行資料互動PHPPython