prevent对话框解雇在Android的屏幕旋转 [英] Prevent dialog dismissal on screen rotation in Android
问题描述
我想从活动时重新启动被解雇建有警报建设者prevent对话框。
I am trying to prevent dialogs built with Alert builder from being dismissed when the Activity is restarted.
如果我重载onConfigurationChanged方法我能成功地做到这一点,重新布局,以正确的方向,但我失去的EditText粘文本特征。因此,在解决对话框的问题,我创造了这个的EditText问题。
If I overload the onConfigurationChanged method I can successfully do this and reset the layout to correct orientation but I lose sticky text feature of edittext. So in solving the dialog problem I have created this edittext problem.
如果我救了弦从EditText上,并重新分配他们在onCofiguration变化,他们似乎仍然默认为初始值旋转前没有输入的内容。即使我强制无效似乎并更新它们。
If I save the strings from the edittext and reassign them in the onCofiguration change they still seem to default to initial value not what was entered before rotation. Even if I force an invalidate does seem to update them.
我真的需要解决或者对话框的问题或问题的EditText
I really need to solve either the dialog problem or the edittext problem.
感谢您的帮助。
推荐答案
要避免这个问题现在最好的办法是使用<一个href="http://developer.android.com/reference/android/app/DialogFragment.html"><$c$c>DialogFragment$c$c>.
The best way to avoid this problem nowadays is by using a DialogFragment
.
创建延伸<一个新的类href="http://developer.android.com/reference/android/app/DialogFragment.html"><$c$c>DialogFragment$c$c>.覆盖<一href="http://developer.android.com/reference/android/app/DialogFragment.html#onCreateDialog%28android.os.Bundle%29"><$c$c>onCreateDialog$c$c>并返回旧 对话框
或<一href="http://developer.android.com/reference/android/app/AlertDialog.html"><$c$c>AlertDialog$c$c>.
然后你可以用<一个显示它href="http://developer.android.com/reference/android/app/DialogFragment.html#show%28android.app.FragmentManager,%20java.lang.String%29"><$c$c>DialogFragment.show(fragmentManager,标签) 。
Then you can show it with DialogFragment.show(fragmentManager, tag)
.
下面是一个<一个例子href="http://developer.android.com/guide/components/fragments.html#EventCallbacks"><$c$c>Listener$c$c>:
public class MyDialogFragment extends DialogFragment {
public interface YesNoListener {
void onYes();
void onNo();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof YesNoListener)) {
throw new ClassCastException(activity.toString() + " must implement YesNoListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.dialog_my_title)
.setMessage(R.string.dialog_my_message)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((YesNoListener) getActivity()).onYes();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((YesNoListener) getActivity()).onNo();
}
})
.create();
}
}
而在您调用活动:
And in the Activity you call:
new MyDialogFragment().show(getSupportFragmentManager(), "tag"); // or getFragmentManager() in API 11+
这答案混合这三个问题(和他们的答案):
This answer mixes these three questions (and their answers):
- Android Best way of avoid Dialogs to dismiss after a device rotation
- Android DialogFragment vs Dialog
- How can I show a DialogFragment using compatibility package?
这篇关于prevent对话框解雇在Android的屏幕旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!