0

I'm new on Android programming.

I want to create some ArrayLists and put them into the program. When I close or pause the program they must be stored. Until the program uninstalled.

How can I store them in the program.

Can I use savedInstanceState?

Erdinç Özdemir
  • 1,363
  • 4
  • 24
  • 52

5 Answers5

2

you can use Shared preferences for store data and get data for more details see offical documentation on below http://developer.android.com/reference/android/content/SharedPreferences.html

and this link for sample tutorial for shared preferences http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/

Stack Overflow User
  • 4,052
  • 6
  • 29
  • 47
1

Ways to store array List .

Option 1. I referred to this link Save ArrayList to SharedPreferences

After API 11 the SharedPreferences Editor accept Sets. You could convert your List into a HashSet . When your read it back, convert it into an ArrayList . Check the above link for complete explaination and code .

//Retrieve the values
Set<String> set = new HashSet<String>();
set = myScores.getStringSet("key", null);

//Set the values
Set<String> set = new HashSet<String>();
set.addAll(listOfExistingScores);
scoreEditor.putStringSet("key", set);
scoreEditor.commit();

Option 2. If you have no. of arrayLists and instead of using option one ,you can save the data in a table using sqlite .Sqlite is preferred when you have bulky and relational data. you can check this sqlite tutorial

Option 3. Use static arraylists .Static variables retain values but not for very long .Its not a good practice to use static variables .

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
0

Please check this link on developer site

http://developer.android.com/guide/topics/data/data-storage.html

Your data storage options are the following:

Shared Preferences

Store private primitive data in key-value pairs.

Internal Storage

Store private data on the device memory.

External Storage

Store public data on the shared external storage.

SQLite Databases

Store structured data in a private database.

Network Connection

Store data on the web with your own network server.

Sunny
  • 1,441
  • 2
  • 13
  • 22
0

Use the SQLite install that comes integrated with Android. This will persist until the user uninstalls/wipes their data SQLite This tutorial has good examples

jhorvat
  • 385
  • 3
  • 14
0

I can think of two ways to store data persistent to use till you uninstall the application:

Store it in DB Store it in Shared Preference File.

As you said you are new to android, I would prefer you to use SharedPreference.

Declare the preference object like this

SharedPreferences prefs = this.getSharedPreferences(context, Context.MODE_PRIVATE);

To store the value in the key using

prefs.edit().putString(KEY, "Value to be stored").commit();

To get the value use

prefs.getString(KEY, DEFAULT_VALUE);

If the KEY doesn't have a value-pair then the DEFAULT_VALUE is returned.

Naresh
  • 3,174
  • 1
  • 17
  • 22