我試圖從AFragment傳遞MyData(Parcelable對象,包括ArrayList)到BFragment。 當然,它工作。但是,ArrayList的引用是相同的。在調用值的片段之間傳遞數據不參考
我看過關於Parcelable的各種文章,最後我發現只有當兩個對象在不同的任務中時,Parcelable纔會重新創建實例。
如何傳遞數據與原始類型不參考類型?
我寫了如下代碼。
邁德特:
public class MyData implements Parcelable {
public List<String> mList = new ArrayList<>(0);
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeStringList(mList);
}
public static final Parcelable.Creator<MyData> CREATOR = new Parcelable.Creator<MyData>() {
public MyData createFromParcel(Parcel in) {
return new MyData(in);
}
public MyData[] newArray(int size) {
return new MyData[size];
}
};
private MyData(Parcel in) {
in.writeStringList(mList);
}
public MyData() {
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SampleFragment sampleFragment = new SampleFragment();
replaceFragment(sampleFragment);
}
public void replaceFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
if (manager.findFragmentByTag(fragment.getClass().getCanonicalName()) != null) {
return;
}
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container_layout, fragment, fragment.getClass().getCanonicalName());
transaction.addToBackStack(null);
transaction.commit();
}
}
AFragment:
public class SampleFragment extends Fragment implements View.OnClickListener {
MainActivity mActivity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = (MainActivity) context;
}
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sample1, container, false);
view.findViewById(R.id.button).setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.button) {
List<String> list = new ArrayList<>(0);
list.add("sample1");
list.add("sample2");
list.add("sample3");
HogeData data = new HogeData();
data.mList = list;
SampleFragment2 sampleFragment2 = new SampleFragment2();
Bundle bundle = new Bundle();
bundle.putParcelable(sampleFragment2.getClass().getCanonicalName(), data);
sampleFragment2.setArguments(bundle);
mActivity.replaceFragment(sampleFragment2);
}
}
}
BFragment:
public class SampleFragment2 extends Fragment {
MainActivity mActivity;
HogeData mData;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = (MainActivity) context;
}
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
mData = bundle.getParcelable(getClass().getCanonicalName());
}
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
return inflater.inflate(R.layout.sample2, container, false);
}
的問題是,當我通過從AFragment數據(邁德特)至BFragment,數組列表的(mList)參考是相同的。這導致當我改變BFragment中mList的值時,AFragment中mList的值也會改變。 我不知道要解決這個問題。
您需要創建一個新的MyData作爲副本,並將其發送到BFragment – cokceken