Reputation: 1746
I know this is very simple, but it was never answered explicitly here: Want to display a dialog only once after the app been installed. Could someone give an example of the display the dialog call that I could make within my MainActivity class?
Specifically I need to check to see whether that value exists within a Parse Database. If it does, then allow the user to continue. Otherwise do not close out of the dialog box and wait for them to add a new username.
The XML would look something like this:
<PreferenceCategory android:title="@string/pref_user_profile" >
<EditTextPreference
android:key="prefUsername"
android:summary="@string/pref_user_name_summary"
android:title="@string/pref_user_name" />
</PreferenceCategory>
Upvotes: 0
Views: 492
Reputation: 2040
Check if preferences has returned "default," if so then show a dialog box and commit those values to the preferences.
Editor editor = prefs.edit();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String p = prefs.getString("prefUsername", "default");
if (p.equals("default")) {
createDialogBox();
p = prefs.getString("prefUsername", "default");
}
public void createDialogBox()
{
final EditText et = new EditText(this);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// set title
alertDialogBuilder.setTitle("Please give Input");
// set dialog message
alertDialogBuilder
.setMessage("Input")
.setView(et)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String c = et.getText().toString().trim();
editor.putString("prefsUsername", c);
editor.commit();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
Upvotes: 1
Reputation: 483
well it was answered in that question!
Use SharedPreference to store the firstrun value, and check in your launching activity against that value. If the value is set, then no need to display the dialog. If else, display the dialog and save the firstrun flag in SharedPreference. ex (in your launching activity):
public void onCreate(){
boolean firstrun = getSharedPreference("PREFERENCE", MODE_PRIVATE).getBoolean("firstrun", true);
if (firstrun){
...Display the dialog
// Save the state
getSharedPreference("PREFERENCE", MODE_PRIVATE)
.edit()
.putBoolean("firstrun", false)
.commit();
}
}
Upvotes: 0