I think there is something I didn't really understand in the onSaveInstanceState mechanism: I have a class which contains several datas:
public class Site implements Serializable {
private static final long serialVersionUID = 1L;
// used to save device language when the application starts
private String deviceLanguage;
private int deviceWifiState;
private int deviceWifiNetworkId;
private String localeLanguage;
// URLs to access various files to describe site and points of interests
private String wifiSSID = null;
private String wifiWPAKey = null;
private String url = null;
private String flagsDir = "flags/";
private String flagsUrl = null;
private String mediasDir = "medias/";
private String mediasUrl = null;
private String descriptionFileUrl = null;
private FragmentActivity activity;
private ArrayList<Language> listLanguages = null;
private ArrayList<PointOfInterest> listPointsOfInterest = null;
...
// and some getter/setter
}
So the MainActivity
inflate a Fragment_init
which starts to fill in the Site class.
Mainly the ArrayList.
This Fragment_init
is not in the back stack.
When the job is done, it automatically commit() a new Fragment_selectLanguage
where the user set its language before continuing the initialisation.
Once done, this thread is put in the back stack and a third Fragment_play() in commited.
All fragments implement the following onsaveInstanceState
method, with the Bundle getted back on their onViewCreated
method.
@Override
public void onSaveInstanceState(Bundle saveInstanceState) {
super.onSaveInstanceState(saveInstanceState);
saveInstanceState.putSerializable(FRAGMENT_SELECT_LANGUAGE_SITE, (Serializable) site);
}
While in the third fragment, if I change the screen orientation then go back to the previous fragment, I get a null pointer exception when it uses listeLanguages, normally initialized in the first fragment, which is null.
When I examine the Site class, I see that most of the datas are inside but some, often the arrays, are null.
What do I miss? I was thinking that the whole Site class was saved !?
Regards,