Adrian
Adrian

Reputation: 360

Can't dismiss Alert Dialog Android

I am launching an Alert Dialog when the user launches the App, like so:

public class MainActivity extends ActionBarActivity {

final Context context = this;

private SharedPreferences mPrefs;
private Editor mEditor;

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

    mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    mEditor = mPrefs.edit();
    mEditor.apply();

    boolean isUsedBefore = mPrefs.getBoolean("used",false);

    //if(isUsedBefore==false){
        //do nothing
    //}else{
        launchDialogSex();
        //launchDialogBody();
    //}
}

private void launchDialogSex() {
    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);
    final AlertDialog alert = alertDialogBuilder.create();

    // set title
    alertDialogBuilder.setTitle("Körperangaben - Geschlecht")
            .setSingleChoiceItems(R.array.sex_array, -1, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //save chosen sex
                    String[] sex = new String[]{"male","female"};
                    mEditor.putString("sex", sex[which]);
                    //don't open Dialog by next launch
                    mEditor.putBoolean("used", true);
                    mEditor.commit();

                    alert.dismiss();

                    //auto launch next Dialog
                    launchDialogBody();
                }
            });
        alertDialogBuilder.show();
    }

private void launchDialogBody() {
    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.text_entry, null);
    //text_entry is an Layout XML file containing two text field to display in alert dialog
    final EditText input1 = (EditText) textEntryView.findViewById(R.id.mass);
    final EditText input2 = (EditText) textEntryView.findViewById(R.id.height);
    final EditText input3 = (EditText) textEntryView.findViewById(R.id.age);
    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    //create Dialog
    alert
            .setTitle("Körperangaben")
            .setView(textEntryView)
            .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Log.i("AlertDialog","TextEntry 1 Entered "+input1.getText().toString());
                            Log.i("AlertDialog","TextEntry 2 Entered "+input2.getText().toString());
                            Log.i("AlertDialog","TextEntry 3 Entered "+input3.getText().toString());
                            /* User clicked OK so do some stuff */
                            mEditor.putString("mass",input1.getText().toString());
                            mEditor.putString("height",input2.getText().toString());
                            mEditor.putString("age",input3.getText().toString());
                            mEditor.commit();
                            energieBedarf();
                        }
                    })
            .setNegativeButton(R.string.alert_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                        }
                    });
    //show Dialog
    alert.show();
}


public void energieBedarf(){
    String sex = mPrefs.getString("sex","female");
    String massString = mPrefs.getString("mass","65");
    String heightString = mPrefs.getString("height","165");
    String ageString = mPrefs.getString("age","25");

    double mass = Double.parseDouble(massString);
    double height = Double.parseDouble(heightString);
    int age = Integer.parseInt(ageString);

    if(sex.equals("female")){
        double grundUmsatzFemale = 655.1 + (13.7 * mass) + (5 * height) - (6.8* age);
        //TODO add PAL into calculations
        double gesamtUmsatzFemale = (grundUmsatzFemale + (grundUmsatzFemale * (1.2-1)) ) + ((grundUmsatzFemale + (grundUmsatzFemale * (1.2-1)))*0.07);

        Log.e("Ausgabe", String.valueOf(gesamtUmsatzFemale));

    }else{
        //Double grundUmsatzMale = 66.47 + (9.6 * Double.parseDouble(mass)) + (1.8*Double.parseDouble(height)) - (4.7* Integer.parseInt(age));
        //Double gesamtUmsatzMale = (grundUmsatzMale + (grundUmsatzMale * (1.2-1)) ) + ((grundUmsatzMale + (grundUmsatzMale * (1.2-1)))*0.07);
    }
}

Even though I followed several similar (for example) Questions, that seemed to work for the asker, but still my Alert Dialog doesn't close.

Info: The Method is called in onCreate, launchDialogBody launches another Dialog that asks for another set of Body Information.

Upvotes: 0

Views: 1327

Answers (2)

gdaramouskas
gdaramouskas

Reputation: 3747

Take a look at this.

First you create the builder, add buttons, views etc then you call AlertDialog dialog = builder.create(); From what I can see the onClick callback is not even executed.

Upvotes: 1

A.R.
A.R.

Reputation: 2641

Try this

Instead of using

    alert.dismiss();

Replace it by

    dialog.dismiss();

Like this

 @Override
   public void onClick(DialogInterface dialog, int which) {
       //save chosen sex
       String[] sex = new String[]{"male","female"};
         mEditor.putString("sex", sex[which]);
          //don't open Dialog by next launch
           mEditor.putBoolean("used", true);
            mEditor.commit();

             dialog.dismiss();

             //auto launch next Dialog
             launchDialogBody();
     }

Upvotes: 2

Related Questions