Skip to content Skip to sidebar Skip to footer

Showing Preference Screen First Time App Is Run And Related Questions

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 have

Solution 1:

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!

SharedPreferencesprefs= getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
booleanhaveWeShownPreferences= 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

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

Solution 2:

I assume that you're running an emulator, when you start the emulator you have the choice to "wipe saved data" when you start it so it will be like you started it as if you just started the application. Alternatively, you can go into settings -> Applications -> You app -> Wipe data.

In regards to your coding solution, I don't have anything handy at the moment, but what you should do is start your main activity, run a procedure/function to check if the sharedpreference file is empty, and if it is start the preference activity, otherwise run your main activity. Alternatively, instead of checking for the file to be empty, you could see if a value that you are looking for user input (for example UserID) is null or not. If that value isn't null that means that the application can continue.

Solution 3:

I've sorted this out with this bit of code in my main activity

if (prefs.getString("edittextpref", null) == null)
    {
        startActivity(new Intent(this, Preferences.class));
        return;
    }

}

It just checks if one of your values is empty but you need to put this at the bottom of onCreate or else when you go back to the main page it will be blank.

Solution 4:

I am doing something like this. And its works for me.

Stringpath="//data//data//"+this.getPackageName()+"//shared_prefs//feedbackpref.xml";
booleanexists= (newFile(path)).exists(); 
if (exists) {
    introWindowNavigate=false;                                  
}

Post a Comment for "Showing Preference Screen First Time App Is Run And Related Questions"