26

I want to save a flag for recognizing that my app is run for the first time or not. For this simple job I don't want to create database..

Is there a simple option to do this? I want to save and read little pieces of information only.

Joop
  • 3,706
  • 34
  • 55
Fcoder
  • 9,066
  • 17
  • 63
  • 100
  • SharedPreference is the way to go. http://developer.android.com/reference/android/content/SharedPreferences.html – sachinsharma Mar 13 '13 at 12:22
  • The easiest way to persist information like this is by using [`Preferences`](https://developer.android.com/reference/java/util/prefs/Preferences.html). You can find a great introduction on [Saving Key-Value Sets in the documentation](https://developer.android.com/training/basics/data-storage/shared-preferences.html). – Ben Weiss Mar 13 '13 at 12:20

6 Answers6

58

Use sharedPreference or files to save the data but better option is sharedPreference.

For Retrieving

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);

For Saving

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", true);
editor.commit();
Ajay S
  • 48,003
  • 27
  • 91
  • 111
32

Use SharedPreferences.

SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
SharedPreferences.Editor edit= preferences.edit();

edit.putBoolean("isFirstRun", false);
edit.commit();
Rajitha Siriwardena
  • 2,749
  • 1
  • 18
  • 22
10

A proper way to do this is by using the Android class SharedPreferences which is used for things like this.

Storing Settings

SharedPreferences settings = getSharedPreferences(NAME_OF_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("appPreviouslyStarted", true);
editor.apply();

Don't forget to apply or your mutations to the settings won't be saved!

You can create multiple settings by using different NAME_OF_PREFERENCES. The settings are stored on the device so will be available after closing the application.

When you try to retrieve NAME_OF_PREFERENCES that is not already created, you create a new one. See more behavior like this here.

apply() versus commit()

You can use editor.apply() as well as editor.commit(), the only difference is that apply() does not return a boolean value with if the edit was successful or not. editor.apply() is therefor faster and more commonly used.

What is MODE_PRIVATE

You can see all about the different modes here. For your case MODE_PRIVATE is fine.

Retrieving settings

SharedPreferences settings = getSharedPreferences(NAME_OF_PREFERENCES, MODE_PRIVATE);
boolean silent = settings.getBoolean("silentMode", false);

When retrieving a settings from a SharedPreferences object you always have to specify a default value which will be returned when the setting was not found. In this case that's false.

Joop
  • 3,706
  • 34
  • 55
4

I suggest you to go for SharedPreference persistent storage. Its very easy and fast storing/retrival for small amount of information.

See the code to get the value from SharedPreference

   // Restore preferences
   SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
   boolean silent = settings.getBoolean("silentMode", false);
   setSilent(silent);

and to Store value in SharedPreference

 // We need an Editor object to make preference changes.
  // All objects are from android.context.Context
  SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putBoolean("silentMode", mSilentMode);
Raynold
  • 443
  • 2
  • 9
  • 28
3

You can do one class for example: (like a object for instance)

import android.content.Context;
import android.content.SharedPreferences;


public class SettingsMain {

    Context context;
    SharedPreferences preferences;
    SharedPreferences.Editor editor;

    private static final String PREFER_NAME = "settingsMain";
    public static final String KEY_VIBRATE = "switchVibrate";

    public SettingsMain(Context context) {
        this.context = context;
        setPreferences();
    }

    private void setPreferences(){
        preferences = context.getSharedPreferences(PREFER_NAME, context.MODE_PRIVATE);
        editor = preferences.edit();
    }

    public void cleanPreferences(){
        editor.clear();
        editor.commit();
    }

    public void setStatusVibrate(Boolean status){
        editor.putBoolean(KEY_VIBRATE, status);
        editor.commit();
    }
    public Boolean getstatusVibrate(){
        return preferences.getBoolean(KEY_VIBRATE, true);
    }
}

On your activity call:

public class Home extends AppCompatActivity {


    private SettingsMain settings;
    private SwitchCompat switchVibrate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.master);


        setResources();
        getSettings();

    }

    private void setResources(){

        switchVibrate = (SwitchCompat) findViewById(R.id.master_main_body_vibrate_switch);
        switchVibrate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                settings.setStatusVibrate(isChecked);
            }
        });
    }

    private void getSettings(){
        settings = new SettingsMain(this);
        switchVibrate.setChecked(settings.getstatusVibrate());
    }

}
Alex Zaraos
  • 6,443
  • 2
  • 26
  • 21
2

What about using static variables globally?

You can do this as given in this tutorial. I know handling Content providers are unnecessary just to keep some flags.

Else you can check out Shared Preferences provided by Android. Here's a good example to get started.

This would be my recommendation.

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148