2

I want my app to have an activity that shows the PreferencesScreen for setting up the app. This screen shall only be showed once after an install, how can i do this?

i know i can handle with sharedprefs, but how?

regards kai

kai
  • 534
  • 4
  • 12

2 Answers2

3

I think you've a main activity that shows first when your app is executed. You can put in onCreate() a very simple code to load the preference activity only if it's the first time you run the app. Like this:

import android.content.SharedPreferences;
import android.app.Activity;
import android.os.Bundle;

public class ActivityMain extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get shared preferences
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        // first time run?
        if (pref.getBoolean("firstTimeRun", true)) {

            // start the preferences activity
            startActivity(new Intent(getBaseContext(), ActivityEditPreferences.class));

            //get the preferences editor
            SharedPreferences.Editor editor = pref.edit();

            // avoid for next run
            editor.putBoolean("firstTimeRun", false);
            editor.commit();
        }
    }
}

The ActivityEditPreferences is the other activity that loads the default Android preference editor. Hope this can help you.

EDIT: For completeness I show you also the activity that manages the preferences:

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;

public class ActivityEditPreferences extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.preferences);
...

You can get a complete example here: http://www.kaloer.com/android-preferences

And don't forget to look at the official reference: http://developer.android.com/reference/android/preference/PreferenceActivity.html

Seraphim's
  • 12,559
  • 20
  • 88
  • 129
0

You can add a boolean variable to your shared prefs which checks the first launch status. On first launch, set the variable to false.On subsequent launches, you can check the status of the variable to check whether that launch is first launch.

public void OnCreate(Bundle savedInstance)
{
    // your other code

    SharedPreferences sharedPrefs=getSharedPreferences("MyAppPrefs",0);
    // The default value is true as the preference does not exist yet
    boolean isFirstLaunch=sharedPrefs.getBoolean("firstLaunch",true);
    if(isFirstLaunch)
    {
        // An editor so you can write the preference
        SharedPrefrences.Editor editor=sharedPrefs.edit();
        // subsequent launches will get this value as false
        editor.putBoolean("firstLaunch",false);
        editor.commit();
    }
    else
    {
        // Do other work if this is not the first launch
    }

}
sandyiscool
  • 561
  • 2
  • 9
  • thank you for the fast reply. should i put this code in the activity who gets the other userinformations or in the splashscreen? – kai Mar 19 '13 at 08:36
  • That depends on you. If you are having an activity which retrieves other information as you mentioned, then this code may be added to that activity. – sandyiscool Mar 19 '13 at 08:40