After screen orientation change the dialogFragment appears apparently without any call
Asked Answered
A

3

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.

Abra answered 18/2, 2014 at 20:29 Comment(7)
So you start activity, do nothing, rotate the device and dialog appears or you start actitity, click edittext, rotate the device...?Gigahertz
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 ?Hoenir
@Gigahertz no,sorry, if I rotate the screen before opening the dialog, nothing happens.Abra
@Injhb I think is restarted, but I tried also to put android:configChanges="orientation" in the manifest but same results!Abra
@Abra well if you put configChanges and override the method then activity won't be restarted. That was my pointHoenir
@Injhb I put a video sample in gDrive! here it is: linkAbra
Maybe make fragment1 a global variable and set it to null some time after the user has dismissed it.Kun
H
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.

Hoenir answered 18/2, 2014 at 23:11 Comment(3)
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.Daimon
Great comment - as, for me, Jabbar's answer was resulting in java.lang.IllegalStateException: Can not perform this action after onSaveInstanceStateNigeria
@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 resumesHoenir
A
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();
Associationism answered 22/7, 2014 at 6:29 Comment(0)
G
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.

Gigahertz answered 18/2, 2014 at 20:38 Comment(3)
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.Abra
But you said above, that "if I rotate the screen before opening the dialog, nothing happens". Where is mistake?Gigahertz
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)Abra

© 2022 - 2024 — McMap. All rights reserved.