Armit and Sniffer are both correct (I didn't have time to check lucefer's link on serializing the entire array and storing it at one time). You have multiple options here depending on your situation.
As a general "rule of thumb" when storing values in Android:
When you want to temporarily store data while the user has the app open (even when the app is moved to the background), but want the data to reset to original values (0, false, or null if not defined) every time you close your application:
-Utilize SavedInstanceState which will even protect data from screen rotation changes.
OR
-Create a new Java Class and store your data there. Data stored in a Java class file will keep its value as long as the app is open (even when the app is moved to the background), where as data stored in an Activity will reset its values on orientation changes or even when moving to another activity if the data is not being explicitly stored or passed... (BELOW IS AN EXAMPLE OF A SINGELTON JAVA CLASS)
public class RegularJavaClass {
public static RegularJavaClass myObj;
public RegularJavaClass(){}
public static RegularJavaClass getInstance() {
if (myObj == null) {
myObj = new RegularJavaClass();
}
return myObj;
}
private int myNumValue;
private String myStringValue;
public int getMyNumValue() {
return myNumValue;
}
public void setMyNumValue(int myNumValue) {
this.myNumValue = myNumValue;
}
public String getMyStringValue() {
return myStringValue;
}
public void setMyStringValue(String myStringValue) {
this.myStringValue = myStringValue;
}
}
THEN WHEN YOU CALL THIS CLASS TO STORE STUFF USE
int someNum = 3000;
String someString = "saveThisString";
RegularJavaClass.getInstance().setMyNumValue(someNum);
RegularJavaClass.getInstance().setMyStringValue(someString);
THEN WHEN YOU WANT TO RETRIEVE THIS DATA
int retrieveNum = RegularJavaClass.getInstance().getMyNumValue();
String retrieveString = RegularJavaClass.getInstance().getMyStringValue();
When you want to store a small amount of data and have it be retained even when the application is closed and opened again at a later time (or until the application has been un-installed or explicitly changed by the developer)
-Use sharedPreferences. Think of it as a tiny database that every android application comes with automatically. Remember only store SMALL amounts of data here
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
SharedPreferences prefs =
getSharedPreferences("StorageName", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//The value type determines the "put" in your case break the
//array into a for statement and save each value
for (String s : myArray) {
//s would be the value
//make a key for your stored value just like a HashMap
editor.putString(key, value).apply();
}
}
});
t.start();
}
}
THEN TO RETRIEVE YOUR STORED VALUES LATER
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs =
getSharedPreferences("StorageName", MODE_PRIVATE);
//Always enter a default value for just in case the storage
//value is empty
String myStoredValue = prefs.getString(key,null);
}
}
When you want to store a LARGE amount of data and have it be retained even when the application is closed and opened again at a later time (or until the application has been un-installed or explicitly changed by the developer)
-Use a database such as SQLite