Showing preference screen first time app is run and related questions

后端 未结 4 1766
深忆病人
深忆病人 2021-02-06 08:34

I have an app with 2 activities, the preference and the main activity, I need the preference screen to show first time the app is run so the user can do some configuration. I ha

4条回答
  •  旧时难觅i
    2021-02-06 09:09

    1) When your main activity starts check a boolean preference with the default set to false. If it is false, launch your preference activity, if it is true then you know you have saved it to be true!

    SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
    boolean haveWeShownPreferences = prefs.getBoolean("HaveShownPrefs", false);
    
    if (!haveWeShownPreferences) {
        // launch the preferences activity
    } else {
       // we have already shown the preferences activity before
    }
    

    2) In your preferences activity save the same boolean preference with a value of true in onCreate

    SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
    SharedPreferences.Editor ed = prefs.edit();
    ed.putBoolean("HaveShownPrefs", true);
    ed.commit();
    

提交回复
热议问题