Search code examples
javaandroidandroid-alertdialogandroid-preferences

How to let alertDialog pop-out every time I run my app?


After installing the app, an alertDialog will pop-out. I wanted it to have a edit text field asking for the name of the user. After answering, it will never show again. Then afterwards, every time I open the app, another alertDialog will pop-out like a greeting to the user.

public class MainActivity extends Activity {
    final Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle("Welcome");
        alert.setMessage("Enter Your Name Here"); 
        final EditText input = new EditText(context);
        alert.setView(input);
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            } 
        });

        AlertDialog alertDialog = alert.create();
        alertDialog.show();

        AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity.this);
        a_builder.setMessage("Mabuhay!")
                .setCancelable(true)
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = a_builder.create();
        alert.show();
    }
}

Solution

  • After the user has entered his name on the first open of your app, you can write the given username into the SharedPreferences of your Application.

    When a user now opens the app at any other time, you can simply check if you have an entry in the sharedpreferences of your application, and greet the user with the appropriate user name.

    //instance variable in the Mainactivity.class
    private boolean showMessage = true;
    
    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    String username = sharedPref.getString("username", null);
    if(username == null){
        //first time user - ask for username
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("username", enteredName);
        editor.commit();
        showMessage = false;
    } else if(showMessage) {
        showMessage = false;
        //greet
        AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
        alert.setMessage("Hello " + username + "!")
                .setCancelable(true)
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        alert.create().show();
    }