0

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,

LW001
  • 2,452
  • 6
  • 27
  • 36
fralbo
  • 2,534
  • 4
  • 41
  • 73

2 Answers2

0

Because you can not serialize custom objects.

See this question for exact implementation (Second answer) , store and retrieve a class object in shared preference

Using a third party import called Gson , https://github.com/pchauhan/StoreandRetrieveObjectClassDemo

Community
  • 1
  • 1
johng
  • 825
  • 1
  • 9
  • 15
  • Thanks @Alex I'd rather not spend a large chunk of my time developing such a feature for a tiny part of my project. – johng Jul 25 '14 at 21:47
0

So in fact I changed Site from Serializable to Parcelable and then that works perfectly using:

@Override
public void onSaveInstanceState(Bundle saveInstanceState) {
    super.onSaveInstanceState(saveInstanceState);
    Log.d("2ndGuide", "MainActivity: onSaveInstanceState");
    saveInstanceState.putParcelable(MAIN_ACTIVITY_SITE, site);
}

and

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("2ndGuide", "MainActivity: onCreate");
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        site = new Site();
    } else {
        site = (Site)savedInstanceState.getParcelable(MAIN_ACTIVITY_SITE);
    }
}
fralbo
  • 2,534
  • 4
  • 41
  • 73