I have a object, that has other objects inside. I need to pass that object to a FragmentDialog, so I made that object Serializable and it's working perfectly in my device.
public static SelectTeamDialog newInstance(Data data) {
SelectTeamDialog dialog = new SelectTeamDialog();
Bundle args = new Bundle();
args.putSerializable(ARG_PARAM1, data);
dialog.setArguments(args);
return dialog;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
data = (Data) getArguments().getSerializable(ARG_PARAM1);
}
}
The problem is that I'm seeing some NotSerializableException
reported in google developer console, so for some reason, in some devices, is giving NotSerializableException
.
It's important to say that Data object is very big, and has a lot of objects inside, with a lot of objects inside. And not all those objects are serializable, but I noticed that when you are passing the bundle to a Fragment or DialogFragment is not necessary that all the sub-objects contained in a serializable objects are serializable to pass that parent object with putSerializable in a bundle.
I know it because it's working in my device and 95% of the devices. But if I try to pass this serializable object with non serializable objects inside to an Activity with a bundle, it requires to make all the sub objects serializable too (I can't do it because then it gives outofmemory when serializing). Why is not necessary when passing it to a Fragment but it's necessary when working with Activities?
And why it's this giving me some exceptions reported in the developer console if it's working for me and for the 95% of the devices that are using this application?
If the Serializable
really gets serialized, then you will crash with a NotSerializableException
when the serialization encounters something that it cannot handle.
With an Intent
extra, when you use the Intent
to start an activity, start a service, or send a system broadcast, all extras will get serialized. Doing any of those things involves inter-process communication (IPC), even if the resulting activity/service/receiver happens to be in your app. So, when you pass a partially-Serializable
object as an Intent
extra, you will crash with a NotSerializableException
quickly.
Fragment arguments are not necessarily serialized. They will be if onSaveInstanceState()
is called, as arguments automatically become part of your saved instance state. It is possible that a fragment might not get called with onSaveInstanceState()
very often, and so a partially-Serializable
object as an argument might survive. However, inevitably, somebody will press HOME or otherwise switch to another app, and onSaveInstanceState()
will get called. Then, your partially-Serializable
object argument will give you a NotSerializableException
.