views:

101

answers:

1

I would like to know if the user is using the application for the first time. I am using SharedPreferences, but I am not sure if I have the right logic.

How can I set my isFirstLaunched boolean to true when the user first launches, and then immediately set to false after work has been done?

protected void onStart() {
        super.onStart();

        if(isFirstLaunch()){
            populateDefaultQuotes();

            //Save the preferences, isFirstLaunch will now be false
            SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("isFirstLaunch", false);
            editor.commit();
        }

        setupUI();
        checkOrientation();
        restoreCache();
    }

    private void populateDefaultQuotes(){
        System.out.println("!!!!!! FIRST TIMER !!!!!!");
    }

    private boolean isFirstLaunch() {
        // Restore preferences
        SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, 0);
        boolean isFirstLaunch = settings.getBoolean("isFirstLaunch", false);

        return isFirstLaunch;
    }
+5  A: 

Replace the false argument in getBoolean() by true, then the logic will fit.

boolean isFirstLaunch = settings.getBoolean("isFirstLaunch", true); 

This will return true if there is no such setting.

BalusC