9

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.

Gasta87
  • 225
  • 5
  • 16
  • So you start activity, do nothing, rotate the device and dialog appears or you start actitity, click edittext, rotate the device...? – nikis Feb 18 '14 at 20:33
  • Are you sure that your activity is being restarted on rotation ? Have you added any code which may prevent the activity to be restarted ? like configuration changes in manifest ? – shaktiman_droid Feb 18 '14 at 20:33
  • @nikis no,sorry, if I rotate the screen before opening the dialog, nothing happens. – Gasta87 Feb 18 '14 at 20:39
  • @Injhb I think is restarted, but I tried also to put android:configChanges="orientation" in the manifest but same results! – Gasta87 Feb 18 '14 at 20:41
  • @Gasta87 well if you put configChanges and override the method then activity won't be restarted. That was my point – shaktiman_droid Feb 18 '14 at 20:46
  • @Injhb I put a video sample in gDrive! here it is: [link](https://drive.google.com/file/d/0B-fZ6n5oCTEHYUtwY3d3RmxPX0E/edit?usp=sharing) – Gasta87 Feb 18 '14 at 22:34
  • Maybe make fragment1 a global variable and set it to null some time after the user has dismissed it. – danny117 Feb 18 '14 at 22:54

3 Answers3

8

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.

shaktiman_droid
  • 2,368
  • 1
  • 17
  • 32
  • 1
    I can confirm that this method works, but in my situation I've encountered this issue simply because I forgot to include this line: `super.onDismiss(dialog);` in `onDismiss` method. Maybe this will help someone. – Stan Mots Sep 28 '16 at 20:40
  • Great comment - as, for me, Jabbar's answer was resulting in `java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState` – ban-geoengineering Nov 10 '17 at 17:37
  • @ban-geoengineering it's not related to my answer. you're probably calling `commit` when activity is not in resumed state. If you're calling that during `onActivityResult` then don't do it there, instead make sure you call it only after activity resumes – shaktiman_droid Nov 12 '17 at 07:11
7

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();
Driedshrimp
  • 121
  • 1
  • 3
0

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.

nikis
  • 11,166
  • 2
  • 35
  • 45
  • No, I haven't the dialogFragment shown before rotation. it appears again when the screen is rotated if it has been opened and closed once a time. – Gasta87 Feb 18 '14 at 20:43
  • But you said above, that "if I rotate the screen before opening the dialog, nothing happens". Where is mistake? – nikis Feb 18 '14 at 20:57
  • There are two cases: 1) I don't onLongClick ever the editText 2) I onLongClick the editText at least once. In the first case when the screen orientation change the dialog doesn't appear. In the second case, when the dialog is already closed by clicking OK button that produce dialog.dismiss(), when the screen orientation change magically appear again the dialog (like if I onLongClick the editText) – Gasta87 Feb 18 '14 at 20:59