0

I have a bit of usual problem here. I have a alertdialog that launches as soon as my application has been launched and as soon as the user clicks the ok button that dialog will never display again unless it has been deleted and installed again. It works when I try it on my emulator for the first time and by first time I mean when I launch the application as soon I am done writing the code for the shared preference for the alertdialog. But when I close the emulator and launch my application again, the alertdialog doesn't display and my application doesn't respond to anything. I do not know if this happened to anybody before and I do not know if this was suppose to happen. Can somebody help me understand what is going and why application doesn't respond to anything after the first time the application has been launched. Also my logcat did not display any errors either.

public class MainActivity extends Activity {


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



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

        if(!installed){

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

            alertDialog.setTitle("Title");
            alertDialog.setIcon(R.drawable.ic_launcher);
            alertDialog.setAdapter(new MyAdapter(), null);

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

                public void onClick(DialogInterface dialog, int which) {
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("installed", true);
                    editor.commit();

                }
            });

            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();
    }}
4

3 回答 3

1

You need to move this code

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

            }
        });
    }

out of the if part of your code. As Shobhit is saying, this is never getting run the next time you run your app. It only runs if installed is false which is never true after the first run.

Edit-Avoid window leak errors with the Dialog

You can always check if the Dialog is open with dialog.isShowing() and close the Dialog if returns true before the Activity is destroyed or something (another Activity) comes on top. You can do this in onPause().

@Override
public void onPause()
{
    if (dialog != null && dialog.isShowing())
    {    
         dialog.dismiss();    
         super.onPause();
    }
}
于 2013-09-16T22:59:54.423 回答
0

Move this code into an AsyncTask. It's not good practice to do any work in the onCreate(). The OnCreate() is only for creating the Activity. Start your AsyncTask in the OnResume() Look at this activity-life-cycle Here is the AsyncTask

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

if(!installed){

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

alertDialog.setTitle("Title");
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setAdapter(new MyAdapter(), null);

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

    public void onClick(DialogInterface dialog, int which) {
           SharedPreferences.Editor editor = settings.edit();
           editor.putBoolean("installed", true);
           editor.commit();

    }
 });

alertDialog.show();
于 2013-09-13T23:32:21.183 回答
0

What you have is as follows:

if(!installed){
    // Show dialog 
    // Else Everything inside it
}

When your app starts first time, it goes inside loop and shows the dialog. Next time when you restart, the value the SharedPreference is true, so it does not go inside the loop and nothing happen. On restarting the phone/emulator ShraredPreference does not delete, so is still true and does not go inside the loop, so nothing happens.

If you do the indentation of your program clearly, then it might be visible in bettr way.

于 2013-09-13T23:36:58.430 回答