What Is the Most Appropriate Way to Store User Settings in Android Application

What is the most appropriate way to store user settings in Android application

In general SharedPreferences are your best bet for storing preferences, so in general I'd recommend that approach for saving application and user settings.

The only area of concern here is what you're saving. Passwords are always a tricky thing to store, and I'd be particularly wary of storing them as clear text. The Android architecture is such that your application's SharedPreferences are sandboxed to prevent other applications from being able to access the values so there's some security there, but physical access to a phone could potentially allow access to the values.

If possible I'd consider modifying the server to use a negotiated token for providing access, something like OAuth. Alternatively you may need to construct some sort of cryptographic store, though that's non-trivial. At the very least, make sure you're encrypting the password before writing it to disk.

Saving user information in app settings

First of all save user info like uname & password in SharedPreferences with this code.

SharedPreferences settings = getSharedPreferences("UserInfo", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("Username",txtUname.getText().toString());
editor.putString("Password",txtPWD.getText().toString());
editor.commit();

and then get SharedPreferences from below code

SharedPreferences settings = getSharedPreferences("UserInfo", 0);
txtUname.setText(settings.getString("Username", "").toString());
txtPWD.setText(settings.getString("Password", "").toString());

What's the best way to do application settings in Android?

Many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs.

Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity.

  1. Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.

  2. Activity handled preferences: These preferences can only be used with in the activity and can not be used by other components of the application.

Shared Preferences:

The shared preferences are managed with the help of getSharedPreferences method of the Context class. The preferences are stored in a default file(1) or you can specify a file name(2) to be used to refer to the preferences.

(1) Here is how you get the instance when you specify the file name

public static final String PREF_FILE_NAME = "PrefFile";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application. Other two mode supported are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE other application can read the created file but can not modify it. In case of MODE_WORLD_WRITEABLE other applications also have write permissions for the created file.

(2) The recommended way is to use by the default mode, without specifying the file name

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

Finally, once you have the preferences instance, here is how you can retrieve the stored values from the preferences:

 int storedPreference = preferences.getInt("storedInt", 0);

To store values in the preference file SharedPreference.Editor object has to be used. Editor is the nested interface of the SharedPreference class.

SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

Editor also support methods like remove() and clear() to delete the preference value from the file.

Activity Preferences:

The shared preferences can be used by other application components. But if you do not need to share the preferences with other components and want to have activities private preferences. You can do that with the help of getPreferences() method of the activity. The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name.

Following is the code to get preferences

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);

The code to store values is also same as in case of shared preferences.

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

You can also use other methods like storing the activity state in database. Note Android also contains a package called android.preference. The package defines classes to implement application preferences UI.

To see some more examples check Android's Data Storage post on developers site.

Which way of saving user data should I use for my Android app?

If you're using Google's MVVM you can try to use SavedState for ViewModel
This way your app state can survive death of the app process. But the state must be lightweight. If you have quite a lot of data on your screen you should also use Room or file cache or something else. You can read about saving states here

which way is best to store our application settings in Android

You should use SharedPreferences.

For an example and more information see the android documentation: http://developer.android.com/guide/topics/data/data-storage.html

The best place to store a user-accessible configuration file

Store it in getExternalFilesDir(). This would work only if the device has an external storage. The user would be able to access it.

However, take note of the following from the docs:

External files are not always available: they will disappear if the
user mounts the external storage on a computer or removes it. See the
APIs on Environment for information in the storage state.

Which way is preferred to store my android application settings to access them via phonegap and java file

You can use the regular options menu and PreferenceActivity when using PhoneGap that will let your app to easily store Settings in the SharedPreferences.

If you need to read settings from your web page just use the JavaScriptInterface to communicate between the page and native.

Activity onCreate:

bridge = new PGBridge(this);
appView.addJavascriptInterface(bridge, "bridge");

public static class PGBridge{
private DroidGap droidGap;
public PGBridge(DroidGap droidGap){
this.droidGap = droidGap;
}
public String getPreference(String preference){
return PreferenceManager.getDefaultSharedPreferences(droidGap).getString(preference, "");
}

In JS:

var preference = window.bridge.getPreference("something");

Android - How can I save user setting preferences even after the user closes the app with KOTLIN?

get the sharedPreferences of the app in a fragment

mSharedPreferences = requireActivity().getSharedPreferences("createAKey" , Context.MODE_PRIVATE)

how to get a saved sharedPreference, the second parameter is like a else
if you dont have saved before

var email = mSharedPreferences.getString("email_key" , "")

this is how you save a sharedPreference

mSharedPreferences.edit {
val email = "myEmail@email.com"
putString("email_key" , email)
}

}

What is the most appropriate way to store password in Android?

There is a mechanism to store application data in Shared Preferences. But those are not encrypted.

scottyab has created one library which stores secure data in Shared Preferences.

This is Android Shared preference wrapper that encrypts the keys and values of Shared Preferences using 256-bit AES. The key is stored in the perferences and so can be read and extracted by root user. Keys and values are encrypted and base64 encooded before storing into prefs.

Sample Image

References

  • secure-preferences
  • SecurePreferences Demo


Related Topics



Leave a reply



Submit