解决android - Using bundle for message passing in sliding tab fragments returns null
2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址:https://cloud.tencent.com/act/cps/redirect?redirect=1062
2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址:https://www.aliyun.com/minisite/goods
I am new at using fragments. This is how I am passing StringArrayList inside bundle in the onActivityCreated of first fragment in sliding tab
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
next_personal = (Button) getActivity().findViewById(R.id.personal_next);
next_personal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validate();
}
});
}
private void validate() {
isValid = FormValidator.validate(this, new SimpleErrorPopupCallback(getActivity().getApplicationContext(), true));
if (isValid) {
arrayList = new ArrayList<String>();
arrayList.add(memID.getText().toString());
arrayList.add(idNumber.getText().toString());
arrayList.add(firstName.getText().toString());
arrayList.add(secondName.getText().toString());
arrayList.add(lastName.getText().toString());
arrayList.add(secondLastName.getText().toString());
Bundle bundle = new Bundle();
bundle.putStringArrayList("personal",arrayList);
Log.d("bundle",": "+bundle.toString());
FragContactInfo frag = new FragContactInfo();
frag.setArguments(bundle);
((RegisterTabActivity) getActivity()).setCurrentItem(1, true);
}
}
And then I am trying to get the ArrayList in the third fragment of sliding tab as below:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null && bundle.containsKey("personal")) {
ArrayList<String> userId = bundle.getStringArrayList("personal");
}
else{
Toast.makeText(getActivity(),"Bundle is null",Toast.LENGTH_SHORT).show();
}
}
It keeps on returning null. Did the same inside onCreateView of both fragments, same result. What am I getting wrong here?
android android-fragments tabs sliding|
this question asked Jul 31 '15 at 10:52 SagePawan 201 1 15 Are you using the same activity for all your fragments? and where you add the fragements to your activity? i guess you're not using the same instance of the fragment. Please post the activity code. – Rami Jul 31 '15 at 14:02 I resolved it by creating a custom method in target fragment which accepts arraylist and called the method from the message sending fragment. The fragments are for sliding tab. So yes, same parent activity for all tab fragments. Thank you for responding :) – SagePawan Aug 1 '15 at 12:42
|
1 Answers
1
解决方法
Using bundle did not help in parsing data between fragments (Somehow)
So I simply created method to set and get data in target fragment.
public void setPdata(JSONObject obj) {
this.personalJSON = obj;
}
public JSONObject getPdata() {
return personalJSON;
}
Then I called the set method to set json in sender fragment.
FragContactInfo frag = new FragContactInfo();
frag.setPdata(json);
And then access the json by simply calling get method.
array1 = getPdata().getJSONArray("args");
If anyone has a more productive solution, please do tell. Happy Coding.
|
this answer answered Sep 2 '15 at 10:36 SagePawan 201 1 15
|
相关阅读排行
- 1Android Https相关完全解析 当OkHttp遇到Https
- 2Android APK反编译详解(附图)
- 3[置顶] Android APK反编译就这么简单 详解(附图)
- 4Android Fragment 真正的完全解析(上)
- 5Android RecyclerView 使用完全解析 体验艺术般的控件