0

I have an array of object instances myClass[] test_array = new myClass[4]; in my Android Application.

The objects attributes which I would like to save and restore later. After the onStop() event of the Activity they should be saved somehow and on the next start of the activity restored.

How could I do that in the best way? XML, Properties, Preferences, ...?

Ludwigm
  • 23
  • 7

5 Answers5

1

One of the best approach is to override onSaveInstanceState(Bundle savedInstanceState)

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    // Save data on recreate or when app is killed
    savedInstanceState.putParcelableArray("YOUR_KEY",array)
}

And restore the values by overriding onRestoreInstanceState(Bundle savedInstanceState)

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);

  // restore saved data
  array = savedInstanceState.getParcelableArray("YOUR_KEY");

}
Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
0

You can save data in Shared Preferences but the limit of shared Preferences is that they only store String, you cant save object in Shared Preferences. But there is a technique through which you can save object. You can serialization object to json and then save it as string and deserialization it to Object when you need to use.

0

The most common way is to use SharedPreferences which persists the information to a file by using SharedPreferences.Editor.

This solution is suiteable for a small amount of data that you can store as key-value pairs (like a properties file).

For more complex data, you can use the sqlite database drectly or through Room.

And you also have the option to save to save any information to a file, private to the application, with whichever format you would like to (xml, json, yml, properties).

SharedPreferences
SharedPreferences.Editor
sqlite
Room
Android: how to write a file to internal storage

Juan
  • 5,525
  • 2
  • 15
  • 26
0

Your activity will start again in two ways

  1. Configuration changes like screen rotation.

  2. You start application freshly.

For the first case you can put your array in the bundle by overriding onSaveInstanceState(Bundle) by calling putParcelableArrayList(String key, ArrayList<? extends Parcelable> value) in this case you have to implement Parcelable interface on your object or implement Serializable on your object and call putSerializable("key",object); for each item in list.

For second case you can use Preferences / files / sqlite database to save your data. You sue sqlite if your data is relational use preferences otherwise , using files is fast but less flexible and doens't provide lots of feature as a database, you should use files to store Media , with the help of a file provider.

Note : In very rare cases when OS needs to kill your app to free up resources the ostop() might not be called use onPause() to be sure you also handle that case, although it is very rare.

Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24
0

You can use the standard solution for Android, it is saveInstanceState, but the best one is to use ViewModel

Armen101
  • 1
  • 1