What is the best way and how to implement global data storage in an application?

0

In an application that has authentication, most of the times it is necessary to store information such as name, email and, depending on the case, the ID of this user. This information can be used in multiple activities. The ID, for example, can be used as a parameter to retrieve information on a remote server.

What is the best way to implement global data storage in an application?

    
asked by anonymous 09.09.2015 / 22:13

1 answer

1

You can use SharedPreferences

For more information, visit the Android documentation

Edit:

If you understand English you can use this StackOverflow English reference

In the free translation:

To get preferences, use the following method in your activity :

SharedPreferences prefs = this.getSharedPreferences(
      "com.example.app", Context.MODE_PRIVATE);

To get the values:

String dateTimeKey = "com.example.app.datetime";

// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

To edit and save values:

Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).apply();

The location where the saved demo application preferences are stored:

<android-sdk-home>/samples/android-<platformversion>/ApiDemos directory
    
09.09.2015 / 22:19