我试图将MyData(Parcelable对象包括ArrayList)从AFragment传递给BFragment。
当然,它有效。但问题是,当我将数据(MyData)从AFragment传递给BFragment时,arraylist(mList)的引用是相同的。这会导致我在BFragment中更改mList的值时,AFragment中的mList值也会发生变化。 我不知道解决这个问题。
我查看了有关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);
}