Android: saving an array to application data

Is it possible to save the entire array (or even ArrayList) into the data of an Android application?

As far as I know, you can only do things like putInt, putBoolean or putString .... But what about more complex data types? Is there any way to do this?

Or do I need to convert the entire array to a string first, and then parse it back into an array?

+4
source share
5 answers

for complex data types use

Bundle bundle = new Bundle(); bundle.putSerializable("key", object); intent.putExtra("bundle", bundle); 

EDit: or to use arrays

  putStringArray(key,value); 
+2
source

I think yuou does not know that there are functions such as putFloatArray (key, value), putStringArray (key, value) . You can use them.

+1
source

If by "application data" you mean SharedPreferences, then no, you can save simple types .

However, you can save the state of the application through the "Actions" onSaveInstanceState / onRestoreInstanceState , as described here: Saving the Android activity state using the instance save state

Please note that this saves the state for activity, so it is best to have a main action in which you save the state of the application.

+1
source

You can serialize almost any form of data and save it as a byte stream of bytes byte[]

After serialization, save with:

 putByteArray("key", serializedData); 
0
source

You need to implement Parcelable. You should take a look at this tutorial Passing an ArrayList through Actions

0
source

Source: https://habr.com/ru/post/1369499/


All Articles