I need to pass a complex object including methods and fields to a Fragment. The complex object implements an Interface IComplexObject
which is then called by the Fragment, so in my Fragment the complex object itself is not visible.
For creating Instances of the Fragment I use the following code, inspired by this post:
public class SimpleContentFragment extends Fragment {
private IComplexObject complexObject;
protected static SimpleContentFragment newInstance(IComplexObject complexObject) {
SimpleContentFragment f = new SimpleContentFragment();
f.complexObject = complexObject;
return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
complexObject.doSomeThings();
}
}
This generally works like expected, however, in some cases when I try to access complexObject
from the Fragment's onCreateView
I get a NullPointerException.
I have experienced this Exception only on a few older devices and some Kindle devices.
Any ideas what I am doing wrong? How I can pass the object into my Fragment?