سؤال

here there is part of the Activity where the screen orientation change:

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

    et = (EditText) findViewById(R.id.editText1);

    et.setOnLongClickListener(new View.OnLongClickListener() 
    {
        @Override
        public boolean onLongClick(View v) 
        {
            Fragment1 dialogFragment = new Fragment1();
            dialogFragment.show(getFragmentManager(), null);
            dialogFragment.setTextDialog(et.getText().toString());
            return true;                
        }
    });        
}

Apparentely it seems that the dialog that will appear inside the DialogFragment should appear just after the onLongClick over the editText (I know that when the screen orientation change the Activity is restarted, but it shouldn't start normally like the first time that is created?)

My problem: when I open at least once the dialog and I close it, after the screen orientation change I have the dialog displayed again on the screen, like if I long-Clicked the editText.

I don't absolutely know why this happens.

I attach also the structure of dialog fragment:

public Dialog onCreateDialog(Bundle savedInstanceState) 
{
    final Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    LayoutInflater adbInflater = LayoutInflater.from(getActivity());

    View eulaLayout = adbInflater.inflate(R.layout.dialog_crypt, null);     
    Button btn_OK = (Button) eulaLayout.findViewById(R.id.btnOK);
    dialog.setContentView(eulaLayout);

    final EditText et = (EditText)eulaLayout.findViewById(R.id.editText2);
    et.setText(textDialog);

    if(et.length()>0)
    {
        et.setText(et.getText().toString() +  " ");
    }

    et.setSelection(et.length());

    btn_OK.setOnClickListener(
            new View.OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                    textDialog = et.getText().toString();
                    ((Main)getActivity()).setTextOnEditText(textDialog);
                    dialog.dismiss();
                }
            });
    return dialog;
}

Thanks so much for the help.

هل كانت مفيدة؟

المحلول

Try removing the dialog from stack using fragment manager instead of just dismissing it.

getFragmentManager().beginTransaction().remove(dialogFragment.this).commit();

By the way, instead of just using a Fragment for your dialog, you should use DialogFragment itself. Checkout: DialogFragment

Also, don't ever call your activity methods like this ( ((Main)getActivity()).setTextOnEditText(textDialog); unless your fragment is a static inner class. Instead, create an interface to talk between fragments and activity.

نصائح أخرى

When screen changes orientation, it calls onSaveInstanceState method, and it saves the state in the Bundle object including the stack. If you dismiss the dialog without clearing this stack, it will then show the dialog when you rotate the phone since this is in the saveInstanceState bundle.

You must clear dialog off the stack with:

getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();

if you use support library for dialog fragment, or

getActivity().getFragmentManager().beginTransaction().remove(this).commit();

When a config change (like rotation) occurs the old Fragment isn't destroyed - it just adds itself back to the Activity when it's recreated (android retains fragments by default). So if you have your DialogFragment shown before rotation, it will instantly show up after rotation.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top