Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 checked through the answers on this topic and they don't seem very clear but I gather it has to do with checking that there are sharedpreference file is empty.

Can someone please give me a code to sort this out and on which activity would I put the code? Also I am still in the developing stage so I already have my preferences setup how do I undo this?

Thanks in Advance

like image 474
Amanni Avatar asked May 05 '11 23:05

Amanni


People also ask

How do I know when I installed an app for the first time?

There's no reliable way to detect first run, as the shared preferences way is not always safe, the user can delete the shared preferences data from the settings! a better way is to use the answers here Is there a unique Android device ID? to get the device's unique ID and store it somewhere in your server, so whenever ...

How does Sharedpreferences work?

Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage.

What is Preferences in Android?

Preferences in Android are used to keep track of application and user preferences. In any application, there are default preferences that can accessed through the PreferenceManager instance and its related method getDefaultSharedPreferences(Context)


3 Answers

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();
like image 104
Joseph Earl Avatar answered Nov 04 '22 00:11

Joseph Earl


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.

like image 30
Sorean Avatar answered Nov 04 '22 01:11

Sorean


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.

like image 24
Amanni Avatar answered Nov 04 '22 01:11

Amanni