3

I have an alert dialog in my application that launches as soon as the application does. I want my alertDialog only to display when the activity is first launched after it has been installed after that I do not want the alertDialog to ever pop up again unless the user deletes the application an d then installs it again. I have tried looking to see how can I achieve this method and tried learning to write the proper code but I just can not seem to get it done properly. So can somebody help me achieve this method and get around this issue.

    public class MainActivity extends Activity {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean initialDialogDisplayed = preferences.getBoolean("InitialDialog", false);
    if (!initialDialogDisplayed) {
        Editor editor = preferences.edit();
        editor.putBoolean("InitialDialog", true);
        editor.commit();


final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        alertDialog.setTitle("Alert");
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.setMessage("Dialog");

        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

            }
         });

        alertDialog.show();
                final EditText et = (EditText) findViewById(R.id.editText1);
                Button getAnswer = (Button) findViewById(R.id.button1);
                getAnswer.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {       
                        if (et.getText().toString().length()==0) {
                            Toast.makeText(getApplicationContext(),"Can't Be Blank!",Toast.LENGTH_LONG).show();             

                        }else{
                            EditText et = (EditText) findViewById(R.id.editText1);
                            String searchTerm = et.getText().toString().trim();         
                            Intent in = new Intent(MainActivity.this, ListView.class);
                            in.putExtra("TAG_SEARCH", searchTerm);
                            startActivity(in);
                        }

                    }
                });
            }
         }


        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
        }}
Inman Douche
  • 139
  • 3
  • 8

2 Answers2

3

You can use shared preferences to store the fact that the dialog has already been displayed. You should save a value on the display of the dialog and read it to check if you need to display again.

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean initialDialogDisplayed = preferences.getBoolean("InitialDialog", false);
    if (!initialDialogDisplayed) {
        Editor editor = preferences.edit();
        editor.putBoolean("InitialDialog", true);
        editor.commit();

        // Display the dialog here
    }
Szymon
  • 42,577
  • 16
  • 96
  • 114
  • the first time I tried this worked but when I restart my emulator and launch the application my activity doesn't respond to anything – Inman Douche Oct 21 '13 at 19:08
  • No not at all but I think it would be fair for me to post the full activity so you can see what's going on – Inman Douche Oct 21 '13 at 22:49
  • Yes, would be better. My answer is very similar to the accepted one - does the accepted one still work? – Szymon Oct 21 '13 at 22:58
  • No, it worked the first time I launched it, but after I launched the emulator again it did not display and my app did not respond to anything... But can you look at this link and look at codeMagic answer, I think this may be the reason for this http://stackoverflow.com/questions/18796561/shared-prefence-for-alert-dialog-is-making-my-application-non-responsive and I also updated my question if you didn't see it – Inman Douche Oct 21 '13 at 23:05
  • Your code still doesn't have the part to show the dialog only once? Also, I cannot check my code (where it works) this week as I'm away from home. – Szymon Oct 22 '13 at 13:29
  • Sorry.. I just added the sharedPreference to the question hopefully you can get a better look at it now.. Enjoy your vacation! (if it is one) :) – Inman Douche Oct 22 '13 at 13:53
  • You need to move your code to start activity out of `onClick`. The dialog is displayed only once and because of that your activity is never started. – Szymon Oct 22 '13 at 13:58
  • So I should move `final EditText et = (EditText) findViewById(R.id.editText1); Button getAnswer = (Button) findViewById(R.id.button1); getAnswer.setOnClickListener(new OnClickListener() {` above sharedPreference because if I move this `@Override public void onClick(View v) {`above my sharedPreference, my dialog will not be displayed until the user clicks on that button – Inman Douche Oct 22 '13 at 19:09
3

You can store a flag value through SharedPreferences.

 SharedPreferences settings = getSharedPreferences("pref_name", 0);
 boolean installed = settings.getBoolean("installed", false);

if(!installed){

   //showDialog

   SharedPreferences.Editor editor = settings.edit();
   editor.putBoolean("installed", true);
   editor.commit();

}

Link : http://developer.android.com/guide/topics/data/data-storage.html#pref

Idipaolo
  • 788
  • 5
  • 11
  • Would it be better if I put `SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("installed", true); editor.commit();` under the `onClick` method of the dialog? – Inman Douche Sep 08 '13 at 18:17
  • It's the same. Once the dialog has gone, you must have the value installed set to true. – Idipaolo Sep 08 '13 at 20:15
  • 1
    Sorry for accepting your answer so late. I just wanted to let you know that everything works fine now. Thank you! – Inman Douche Sep 09 '13 at 21:25