How to save and load data to/from cache when relaunching an app on Android?


Do you have any useful links on this topic? I understand that I will need to create a database in SQlite in the application, enter data there. But how to proceed further? How can I display them from the cache, and not "download" again?


It is not clear from the question how much data (variables, arrays) should be saved when exiting the application?!

If there is not much data, then you can use the mechanism: "SharedPreferences":

Assignment of variables to save:

static final String SAVE_USERID = "save_userid";
static final String SAVE_PASSWORD = "save_password";
public static String un = "MyName", pw = "MyPassword";

Save method:

public void saveSettings() {
    SharedPreferences.Editor ed = getSharedPreferences("setting",MODE_PRIVATE).edit();
    ed.putString(SAVE_USERID, un);
    ed.putString(SAVE_PASSWORD, pw);

Recovery method:

public void loadSettings() {
    un = getSharedPreferences("setting",MODE_PRIVATE).getString(SAVE_USERID, "");
    pw = getSharedPreferences("setting",MODE_PRIVATE).getString(SAVE_PASSWORD, "");

If there is a lot of data, then work with the database is necessary.

Scroll to Top