QLag
QLag

Reputation: 823

AlertDialog and warning message "Window Already Focused"

In my app, I need to get a value from an AlertDialog which displays a list. After an item is selected, I change the button text (the button which displays the AlertDialog), the dialog is dismissed, and i need to do treatment in onResume().

But onResume is not called and i get the warning "Window already focused". I think the cause is that i change the button text from the dialog so i'm already in the window. But i need to go in onResume(à . how to do that ?

AlertDialog :

AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
    mBuilder.setTitle("Type de l'observation");
    mBuilder.setSingleChoiceItems(titles,-1, new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialogInterface, int item) {
            mDescriptor.setmObservationValue(
                    mContext.getResources().getStringArray(R.array.post_values)[item]);
            mObservationButton.setText(titles[item]);
            dialogInterface.dismiss();
            return;
        }
    });
    mDialog = mBuilder.create();

The onClick method : public void onObservationClick(View v) { mDialog.show(); }

and onResume() :

    @Override
protected void onResume() {
    if(!mDescriptor.getmObservationValue().equals(""))
    {
        String value = mDescriptor.getFieldKey();
        Log.v("VALUE : ",value);
        if(value.equals("VentValue"))
        {
            mFieldLayout.setVisibility(View.VISIBLE);
            mUnit.setText("km/h");
        }
        else if(value.equals("PluieValue"))
        {
            mFieldLayout.setVisibility(View.VISIBLE);
            mUnit.setText("mm");
        }   
        else if(value.equals("NeigeValue"))
        {
            mFieldLayout.setVisibility(View.VISIBLE);
            mUnit.setText("mm");
        }
        else if(value.equals("TempValue"))
        {
            mFieldLayout.setVisibility(View.VISIBLE);
            mUnit.setText("°C");
        }
        else if(value.equals("VisValue"))
        {
            mFieldLayout.setVisibility(View.VISIBLE);
            mUnit.setText("m");
        }
        else
        {
            mFieldLayout.setVisibility(View.GONE);
            mUnit.setText("");
        }
    }

Upvotes: 1

Views: 389

Answers (1)

Sam
Sam

Reputation: 86948

It looks like you are forcibly calling onResume(), but you cannot do this. The Activity is not paused to display an AlertDialog, therefor onResume() is not called after the Dialog is dismissed. Simply move the code that you have in onResume() into another method and call this method when the Dialog is closed.

Consider using an OnDismissListener

Upvotes: 1

Related Questions