Question

I am coding using Xamarin and have a question about application settings.

Is it possible to have application wide settings? I will give you an example:

I have a map application. I have a ListView where the user can select if the map is using the Street View, Satellite View or default view.

Depending on the item that is selected in the ListView depends of the map view that is shown.

Where and how can I save this selection such that this value is visible throughout the application and when the user exits the application, this setting can be remembered for when the user starts the application up again?

Thanks in advance

No correct solution

OTHER TIPS

Yes, it's possible and also very easy. Usually you save simple application settings using SharedPreferences. They can be read from anywhere in the app.

For writing.

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sp.edit();
editor.putBoolean("someBoolean", true);
editor.putString("someString", "string");
editor.commit();

For reading

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
boolean someBoolean = sp.getBoolean("someBoolean", false);
boolean someString = sp.getString("someString", null);

I suppose you are familiar to Java I/O and android basic concepts. So here I go:

For data persistance in Android, you have two main solutions:

  • SQLite database
  • File system

I recommend you to use file system, as you don't really need to organize your data with relational constraints and you will probably not have to make a lot of data persist.

Android documentation is very clear on how to save files: http://developer.android.com/training/basics/data-storage/files.html

I recommend you to create a Setting class that contains a HashMap attribute:

public class Settings implements Parcelable{
    public static HashMap<String,String> settings;

    public static void readSettings(){
        //Here you read your settings file and you fill your hashmap
    }

    public static void writeSettings(){
        //Here you iterate through your hashmap and you write your setting files
    }
}

Every activities will have access to the settings, as the attribute/methods are static. Settings will also be synchronized through every activities (if you change a setting in one activity, every other activities will notice).

If you need some clarifications, leave a comment below.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top