1

I am developing an Android application and need to store some objects to the disk for some reason (with the onSaveInstanceState() method) and then retrieve them back again (with the onRestoreInstanceState() method) with Serialization/Parcelable.

As you might know, to make a class as Parcelable, you need to use Parceler Library or write some boilerplate code.

My problem is, that I have some objects that don't write their class. They are a 3rd party or Android-Sdk related Class. For clarity here is my code:

private FragNavController mNavController;
private MenuItem mMenuItem;
private SharedPreferences mPrefs;

@Override
public void onCreate(Bundle savedInstanceState) {
    LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
    setDefaultLanguage(Locale.ENGLISH.toString());
    //Paper.init(getApplicationContext());

    mPrefs = getPreferences(MODE_PRIVATE);
    super.onCreate(savedInstanceState);
    .
    .
    .
}

I use Parceler Library, Xstream ,icepick, paper, Gson and sharedPreferences Method, but in all of them, I fail to save some objects like mNavController and mMenuItem.

Has anyone faced this problem before and know the best solution to save these objects?

Ivar
  • 6,138
  • 12
  • 49
  • 61
linarcx
  • 116
  • 3
  • 17

2 Answers2

0

A non-answer: do not do this.

Serialization of objects is always tricky business. You have to keep an eye on versioning; to ensure that upgrading your app will not break access to previously serialized objects.

That can be hard enough for your own things. But you intend to serialize objects you know nothing about. So any change to that the 3rd party makes to its classes might directly affect your app. The last thing you want to happen is that some update on the customer device that is not related to your product crashes your product; because all of a sudden previously serialized objects can't be deserialized any more.

Long story short: be cautious to preserve the state of your application by "simply" saving all the objects it is using. A much more robust approach would be to step back and identify the "data elements" in your design; and make sure that you can store that data as data; instead of storing objects - as those objects, yes contain your data; but probably many other things you really don't care about. But still you are forced to think about these other things, because you have to (de)serialize them, too.

In other words: A) first you should isolate the data that needs to be persisted; and when you got that B) you look into good ways of persisting that information. If you use your own database, or shared preferences, ... might depend on your requirements.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • thanks a lot for your comment.but in: "A much more robust approach would be to step back and identify the "data elements" in your design; and make sure that you can store that data as data;" Are you Encourage me to setup database for store data? – linarcx Jun 08 '17 at 08:22
0

Did you see this in the Parceler documentation?

Classes without Java source


For classes whose corresponding Java source is not available, one may include the class as a Parcel by using the @ParcelClass annotation. This annotation may be declared anywhere in the compiled source that is convenient. For instance, one could include the @ParcelClass along with the Android Application:

 @ParcelClass(LibraryParcel.class) public class AndroidApplication
 extends Application{
     //... } 

Multiple @ParcelClass annotations may be declared using the @ParcelClasses annotation.

In addition, classes referenced by @ParcelClass may be configured using the @Parcel annotation. This allows the serialization configuration through any parameter available on the @Parcel annotation including the serialization technique or classes to analyze.

One useful technique is the ability to define global custom converters for a type:

 @ParcelClass(
     value = LibraryParcel.class,
     annotation = @Parcel(converter = LibraryParcelConverter.class)) class SomeClass{} 

This allows for fine grained control over a class that isn’t available for direct modification.

https://github.com/johncarl81/parceler#classes-without-java-source

So I think you need to do that, and write a custom converter.

nasch
  • 5,330
  • 6
  • 31
  • 52