Shared Preference for alert dialog is making my application non responsive

前端 未结 3 1882
温柔的废话
温柔的废话 2020-12-12 07:03

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

相关标签:
3条回答
  • 2020-12-12 07:23

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-12 07:27

    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.

    0 讨论(0)
  • 2020-12-12 07:28

    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();
    
    0 讨论(0)
提交回复
热议问题