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

Asked

Viewed 59 times

0

In an application that has authentication, most of the time 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.

How best and how to implement global data storage in an application?

1 answer

1

You can use the SharedPreferences

For more information visit android documentation


Edit:

If you understand English you can use this Stackoverflow English reference


In free translation:

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

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

To take 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 demo application saved preferences are located:

<android-sdk-home>/samples/android-<platformversion>/ApiDemos directory

Browser other questions tagged

You are not signed in. Login or sign up in order to post.