如何將一些數據傳輸到另一個片段?如何將一些數據傳輸到另一個Fragment同樣地,它也是用extras為intents?
3 回答

揚帆大魚
TA貢獻1799條經驗 獲得超9個贊
Bundle
Fragment fragment = new Fragment();Bundle bundle = new Bundle();bundle.putInt(key, value);fragment.setArguments(bundle);
Fragment
onCreate()
Bundle bundle = this.getArguments();if (bundle != null) { int myInt = bundle.getInt(key, defaultValue);}

慕仙森
TA貢獻1827條經驗 獲得超8個贊
public class MyClass implements Serializable { private static final long serialVersionUID = -2163051469151804394L; private int id; private String created;}
Bundle args = new Bundle();args.putSerializable(TAG_MY_CLASS, myClass);Fragment toFragment = new ToFragment(); toFragment.setArguments(args);getFragmentManager() .beginTransaction() .replace(R.id.body, toFragment, TAG_TO_FRAGMENT) .addToBackStack(TAG_TO_FRAGMENT).commit();
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Bundle args = getArguments(); MyClass myClass = (MyClass) args .getSerializable(TAG_MY_CLASS);

慕勒3428872
TA貢獻1848條經驗 獲得超6個贊
使用片段到片段傳遞數據的完整代碼
Fragment fragment = new Fragment(); // replace your custom fragment class Bundle bundle = new Bundle(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); bundle.putString("key","value"); // use as per your need fragment.setArguments(bundle); fragmentTransaction.addToBackStack(null); fragmentTransaction.replace(viewID,fragment); fragmentTransaction.commit();
在自定義片段類中
Bundle mBundle = new Bundle();mBundle = getArguments();mBundle.getString(key); // key must be same which was given in first fragment
- 3 回答
- 0 關注
- 472 瀏覽
添加回答
舉報
0/150
提交
取消