After changing the screen orientation, the Fragment dialog appears, apparently, without any call

here is the part of the Action in which the screen orientation changes:

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; } }); } 

Obviously, the dialog that appears inside the dialog box should appear immediately after onLongClick over editText (I know that when the screen orientation changes, the activity restarts, but it should not start normally, like the first time it was created?)

My problem: when I open the dialog at least once and close it, after changing the screen orientation, the dialog box appears on the screen again, for example, if I clicked on editText for a long time.

I do not quite understand why this is happening.

I also add a dialog fragment structure:

 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; } 

Thank you for help.

+8
android oncreate android-dialogfragment screen-orientation
source share
3 answers

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

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

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

Also, never call your activity methods like ( ((Main)getActivity()).setTextOnEditText(textDialog); if your fragment is not a static inner class. Instead, create an interface for talking between fragments and activity.

+8
source share

When the screen orientation changes, it calls the SaveInstanceState method and saves the state in the Bundle object, including the stack. If you release the dialog without clearing this stack, it will display a dialog box when you rotate the phone, since it is in the saveInstanceState package.

You must remove the dialog from the stack using

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

if you are using a support library for a dialog fragment or

 getActivity().getFragmentManager().beginTransaction().remove(this).commit(); 
+7
source share

When a configuration change occurs (for example, rotation), the old fragment is not destroyed - it simply adds itself back to the Activity when it is recreated (the android saves the fragments by default). Therefore, if you have your DialogFragment shown before rotation, it will be displayed immediately after rotation.

0
source share

All Articles