5 回答

TA貢獻1802條經(jīng)驗 獲得超6個贊
在代碼中,您將捆綁包設(shè)置為未使用的片段變量
創(chuàng)建捆綁包
創(chuàng)建片段
在片段中設(shè)置捆綁包
顯示片段
這是您需要以片段形式傳遞參數(shù)的方式
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SettingsFragment settingsFragment = new SettingsFragment();
if (savedInstanceState == null) {
Bundle bundle = new Bundle();
bundle.putString("my_bundle_key", "Bundle");
settingsFragment.setArguments(bundle);
}
else {
settingsFragment.setArguments(savedInstanceState);
}
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, settingsFragment)
.commit();

TA貢獻1821條經(jīng)驗 獲得超5個贊
在調(diào)用之前,您必須調(diào)用捆綁包,如下所示:fragment
Bundle bundle = new Bundle(); bundleSettings.putString("my_bundle_key", "Bundle"); SetttingsFragment setttingsFragment = new SetttingsFragment(); setttingsFragment.setArguments(bundle); getSupportFragmentManager().beginTransaction() .replace(android.R.id.content, setttingsFragment) .commit();

TA貢獻1805條經(jīng)驗 獲得超9個贊
在您的活動中,您正在創(chuàng)建兩個片段對象,您正在執(zhí)行的第二個對象未附加到視圖且未在使用中。您的片段應(yīng)按如下方式附加到視圖:setargument
Bundle bundle = new Bundle(); bundleSettings.putString("my_bundle_key", "Bundle"); SetttingsFragment setttingsFragment = new SetttingsFragment(); setttingsFragment.setArguments(bundle); getSupportFragmentManager().beginTransaction() .replace(android.R.id.content, setttingsFragment) .commit();

TA貢獻1795條經(jīng)驗 獲得超7個贊
From Activity you send data with intent as:
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
Fragmentclass obj = new Fragmentclass();
obj .setArguments(bundle);
在片段創(chuàng)建視圖方法中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false);
}

TA貢獻1812條經(jīng)驗 獲得超5個贊
活動
AbcFragment fragment = AbcFragment .newInstance(**Value**);
getSupportFragmentManager.beginTransaction()
.replace(R.id.layout_container, fragment)
.commit();
在片段中
public static AbcFragment newInstance(String Value) {
Bundle args = new Bundle();
args.putString("Value", Value);
AbcFragment fragment = new AbcFragment ();
fragment.setArguments(args);
return fragment;
}
添加回答
舉報