11

I'm trying to use Android Application class (MyApplication.java) for storing data in some ArrayLists of strings and ints.

I want that these data get's stored forever, like a database, but without using databases, to simplify my app.

Currently the data get's stored, when I exit from the app, the process of the app still runs in background. But if I kill the process of the app, the data stored on MyApplication.java class get's removed. So I need some kind of function that stores the data from my variables of MyApplication in a file, and another function that restores the data into the variables.

Is there a simple and easy way to solve this?

code resumed:

public class MyApplication extends Application {
    public static String LeagueTable[][] = new String[21][8];
    //COLUMNAS MATRIZ:
    //0     /1   /2   /3   /4   /5   /6   /7
    //nombre/P.J./P.G./P.E./P.P./G.F./G.C./Pts

    public static League LFP = new League();
    public static Category Primera = new Category(20);
    public static int NEQUIPOS=20;
    public static int[][] matriz= new int[NEQUIPOS+1][NEQUIPOS+1]; //esta es la matriz de emparejamientos, representa el calendario
    public static int PlayerTeamID;
    public static boolean ExistingSaveGame=false;//esto es true cuando existe una partida guardada

    //variables candidatas a poner dentro de una de las clases del modelo, como season por ejemplo
    public static int RoundNumber; //jornada actual
    public static boolean SeasonOver=false;//true cuando la temporada ha terminado

    public void onCreate() {
        super.onCreate();
    }

    public void onTerminate() {
        super.onTerminate();
    }

"and a lot of functions that works with the static variables"
Brian
  • 14,610
  • 7
  • 35
  • 43
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

3 Answers3

15

Depending of your context (especially but not only related to the size of your data), you may want to use the Shared Preferences, the internal storage or the external storage (the SD card).

An official guide about that is available here.

Shlublu
  • 10,917
  • 4
  • 51
  • 70
  • shared preferences can store 2 dimension arrays and arraylists with thousands of elements? – NullPointerException Jul 26 '11 at 12:39
  • 2
    No, it is not designed for that: http://developer.android.com/reference/android/content/SharedPreferences.html. If your collection is small (in terms of weight. Let's say less than a megabyte), I'd rather use the internal storage. And if it's bigger, I'd use the external storage. – Shlublu Jul 26 '11 at 12:43
  • 1
    How heavy is your collection? If it was me : >= 1 MB: external storage. Otherwise internal. – Shlublu Jul 26 '11 at 14:19
  • 1
    Shared Preferences only store strings so you'd have to convert your arrays to a json string and store it. Of course then you would need to convert from json to the array when loading you app again. – JJ_Coder4Hire Jul 02 '14 at 19:45
5

The fastest and easiest way is to use SharedPreferences if you don't store complex data.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
3

There are many options to save data locally on the android. See http://developer.android.com/guide/topics/data/data-storage.html for more on data store.

Sandeep
  • 20,908
  • 7
  • 66
  • 106