[英]Transfering data from an activity to a fragment on a button click
我想將數據從帶有編輯文本的活動轉移到帶有列表的片段類,以僅顯示edit_name框。 我希望能夠單擊活動類中的按鈕,並使其保存所有信息並僅顯示edit_name框。 有人知道如何執行此操作嗎?
基本上,您可以使用Intents來執行此操作,下面是一個示例:
https://stackoverflow.com/a/12739968/5552022
否則,您可以使用EventBus之類的庫來高效地處理該過程:
// Create a new Intent object as container for the result
final Intent data = new Intent();
// Add the required data to be returned to the MainActivity
data.putExtra(EXTRA_DATA, "Some interesting data!");
// Set the resultCode as Activity.RESULT_OK to
// indicate a success and attach the Intent
// which contains our result data
setResult(Activity.RESULT_OK, data);
// With finish() we close the DetailActivity to
// return back to MainActivity
finish();
您可以通過捆綁包轉移數據:
在您的活動中,使用以下代碼在捆綁中添加數據:
Bundle bundle = new Bundle();
bundle.putString("key", "your string");
// set Fragmentclass data as fragment arguments
YourFragment fobj = new YourFragment();
fobj.setArguments(bundle);
在Fragment中,您可以獲取數據:
String strtext = getArguments().getString("key");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.