So here I have this code in my Fragment
:
ProductParams params = (ProductParams) getArguments().getSerializable("product_params");
Log.i("TAG", "setupFragment: "+params.getChampionId());
params.setChampionId(100);
params = (ProductParams) getArguments().getSerializable("product_params");
Log.i("TAG", "setupFragment: "+params.getChampionId());
params.getChampionId()
for the first log is 0. But surprisingly the value for the second log is 100 while I expect it to be 0 (the initial value). Why is it so? And how can I have the getArguments().getSerializable("product_params")
return the intial value?
The Bundle class caches deserialized objects, which means getSerializable
will return a reference to the same object every time. If you change a field in that object, you will see that change through every reference.
For this reason, it's not a good idea to pass mutable objects as parameters in bundles, or otherwise. You wouldn't have this problem if there was no ProductParams.setChampionId
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.