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.