You are creating a new Dialog in your onCreate method. onCreate gets called
both the first time the app is run (with savedInstanceState set to null)
and after an orientation (configuration) change, with savedInstanceState
set to the last saved state Bundle.

In the latter case, the system also automatically re-creates the original
dialog fragment as part of the lifecycle. Therefore, the system is
recreating the dialog for you and you are additionally creating a new
instance.

You explicitly check if the dialog already exists in the fragment manager,
which would be the original instance with the text before orientation
change, and you replace it with the new instance, which would be brand new
and not have the text saved from the original.

The simple solution here is to remove checking if the dialog already exists
and just only create the dialog if savedInstanceState is null.


On Tue, Jan 28, 2014 at 1:52 PM, Daniel Rindt <[email protected]>wrote:

> Dear readers,
>
> i experienced a double call to onCreateDialog also to onCreateView in my
> project.
> here is my activity code:
> public class DialogTestActivity extends Activity {
>
>     @Override
>     protected void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>
>         AskForNameDialog d = AskForNameDialog.newInstance(new
> OnNameSetListener() {
>
>             @Override
>             public void onNameSet(AskForNameDialog dialog, String name) {
>             }
>
>         }, getString(R.string.enter_manufacturers_name));
>
>         this.showMyDialog(d);
>     }
>
>     private void showMyDialog(DialogFragment d) {
>         FragmentTransaction ft = getFragmentManager().beginTransaction();
>         Fragment prev = getFragmentManager().findFragmentByTag("dialog");
>         if (prev != null) {
>             ft.remove(prev);
>         }
>         ft.addToBackStack(null);
>         d.show(ft, "dialog");
>     }
>
> }
>
> here is the dialog:
> public class AskForNameDialog extends DialogFragment {
>
>     protected static final String TAG =
> AskForNameDialog.class.getSimpleName();
>
>     private OnNameSetListener mCallback;
>     private String mMessage;
>
>     public AskForNameDialog() {
>     }
>
>     private AskForNameDialog(OnNameSetListener listener, String message) {
>         mCallback = listener;
>         mMessage = message;
>     }
>
>     public static AskForNameDialog newInstance(OnNameSetListener listener,
> String message) {
>         AskForNameDialog f = new AskForNameDialog(listener, message);
>         Bundle b = new Bundle();
>         b.putBoolean("blah", false);
>         f.setArguments(b);
>         return f;
>     }
>
>     @Override
>     public Dialog onCreateDialog(Bundle savedInstanceState) {
>         int padd = convertDpToPixel(20, getActivity());
>         final EditText txt = new EditText(getActivity());
>         txt.setSingleLine();
>
>         final AlertDialog ad = new AlertDialog.Builder(getActivity())
>                 .setTitle(mMessage)
>                 .setPositiveButton(android.R.string.ok, null)
>                 .create();
>         ad.setView(txt, padd, padd, padd, padd);
>
>         ad.setOnShowListener(new DialogInterface.OnShowListener() {
>
>             @Override
>             public void onShow(DialogInterface dialog) {
>
>                 Button b = ad.getButton(AlertDialog.BUTTON_POSITIVE);
>                 b.setOnClickListener(new OnClickListener() {
>
>                     @Override
>                     public void onClick(View v) {
>                         if (!FormUtils.isValid(txt, 3, 50,
> R.string.input_error_min_max_letters))
>                             return;
>
>                         mCallback.onNameSet(AskForNameDialog.this,
> txt.getText().toString());
>                         dismiss();
>                     }
>                 });
>             }
>         });
>
>         return ad;
>     }
>
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         // setCancelable(false);
>     }
>
>     public static int convertDpToPixel(float dp, Context context) {
>         Resources resources = context.getResources();
>         DisplayMetrics metrics = resources.getDisplayMetrics();
>         return (int) (dp * (metrics.densityDpi / 160));
>     }
>
>     public interface OnNameSetListener {
>
>         public void onNameSet(AskForNameDialog dialog, String name);
>
>     }
>
> }
>
> I'm using exactly the same code in another project and there the dialog is
> working well.
> Explaining what happened:
>
>    1. Dialog properly appears with the input field
>    2. enter some letters in the EditText view
>    3. do an orientation change
>    4. the dialog appear but without the previous entered text
>
> In the debugger i put a breakpoint on onCreateDialog() and see that it
> called first containing savedInstanceState with data
> and immediately after the the method is called again with
> savedInstanceState=null.
>
> This happens on every api level. I appreciate your feedback and thanks in
> advance for your support!
> Daniel
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Android Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>



-- 

-------------------------------------------------------------------------------------------------
TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago
transit tracking app for Android-powered devices

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to