storing a user selection that stores itself for use on reload in android app
I've built a dialog that asks the user to pick a city from the 开发者_如何学运维list provided when the application first opens. The dialog works perfectly, however I want to store the user's choice so that when the app is opened a second time, it checks to see if the user has already made a selection previously. If they have, it doesn't display the dialog and defines the city variable as their previously chosen preference. And obviously, if they haven't made a selection previously (because its their first time opening the app or for some reason the app couldn't read the stored preference), it displays the dialog.
Here's my dialog in case this helps:
final CharSequence[] CityChoice = {"Austin", "Dallas/Fort Worth", "Houston", "San Antonio"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setIcon(R.drawable.icon);
alt_bld.setTitle("Select your city");
alt_bld.setSingleChoiceItems(CityChoice, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), "Your city is now set to "+CityChoice[item]+". To change this, go to Menu -> Preferences.", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
edit: Oh, and by the way, although I'm picking up android programming fairly quickly (at least I think I am haha) I have to admit I'm quite new to it. So the more detailed your response the better. Thanks a ton in advance.
You could use SharedPreferences to store and retrieve the setting, which is pretty straightforward as shown here http://developer.android.com/guide/topics/data/data-storage.html
Launching the dialog based on the setting should be trivial. The setting can then be saved in the dialog's listener.
精彩评论