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
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
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
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
}
}