-1

What is the best way to create a data file in Android for saving data like usernames, configuration, and other variables accessible only by the Application?

Is there an official way to do this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ProtectedVoid
  • 1,293
  • 3
  • 17
  • 42
  • You open up a file and do file IO via the normal Java classes. Small bits of key/value configuration can be put in shared preferences. And of course relational data can be put in an SQLite db. – Gabe Sechan Mar 22 '15 at 00:35

1 Answers1

3

Android has some ways for save data.

Configurations are usually handled by Android Preferences. Where you can save your settings, doing something like this:

SharedPreferences settings = getSharedPreferences("APP_PREFS", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", true);
editor.commit();

For recover data, just do something like this:

// Restore preferences
SharedPreferences settings = getSharedPreferences("APP_PREFS", Context.MODE_PRIVATE);
boolean silent = settings.getBoolean("silentMode", false);

If you want to save more advanced type of data(make relationships between data, index, validations, avoid repeating), is better to use a database for it, and android provides an api to handle this using sqlite databases through SQLiteOpenHelper class.

More detailed example about android database you can find in this stackoverflow question: Android SQLite Example

Nilton Vasques
  • 539
  • 5
  • 10