Domanda

Vorrei sapere se l'utente sta utilizzando l'applicazione per la prima volta. Sto usando SharedPreferences, ma non sono sicuro se ho la logica giusta.

Come faccio a impostare il mio isFirstLaunched booleano true quando i primi lanci utente, quindi immediatamente impostate su false dopo il lavoro è stato fatto?

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;
    }
È stato utile?

Soluzione

Sostituire l'argomento false in getBoolean() da true, allora la logica si inserisce.

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

Ciò restituirà true se non v'è tale impostazione.

Altri suggerimenti

È possibile registrare un BroadcastReceiver per ascoltare Intent.ACTION_PACKAGE_FIRST_LAUNCH

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top