Question

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;
    }
Was it helpful?

Solution

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.

OTHER TIPS

you can register a BroadcastReceiver to listen for Intent.ACTION_PACKAGE_FIRST_LAUNCH

http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_FIRST_LAUNCH

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