在Android中屏幕旋转防止对话框解除 [英] Prevent dialog dismissal on screen rotation in Android

查看:247
本文介绍了在Android中屏幕旋转防止对话框解除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我重载了onConfigurationChanged方法,我可以成功地执行此操作并重新设置布局正确的方向,但我失去了edittext的粘贴文本功能。所以在解决对话框问题我创建了这个edittext问题。



如果我从edittext中保存字符串并在onCofiguration更改中重新分配它们,它们似乎默认为初始值,而不是在旋转之前输入的值。即使我迫使无效似乎更新它们。



我真的需要解决对话框问题或编辑文本问题。



感谢您的帮助。

解决方案

现在避免这个问题的最好方法是使用 DialogFragment 。 p>

创建一个新的类,扩展了 DialogFragment 。覆盖 onCreateDialog ,并返回您的旧 对话框 AlertDialog



然后你可以用 DialogFragment.show(fragmentManager,tag)



下面是一个示例,一个 听众

  public class MyDialogFragment extends DialogFragment {

public interface YesNoList ener {
void onYes();

void onNo();
}

@Override
public void onAttach(Activity activity){
super.onAttach(activity);
if(!(Activity instanceof YesNoListener)){
throw new ClassCastException(activity.toString()+必须实现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();
}
})
。创建();
}
}

在您打电话的活动中:

  new MyDialogFragment()。show(getSupportFragmentManager(),tag); //或getFragmentManager()在API 11+ 

这个答案混合了这三个问题(和他们的答案) :




I am trying to prevent dialogs built with Alert builder from being dismissed when the Activity is restarted.

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.

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.

I really need to solve either the dialog problem or the edittext problem.

Thanks for the help.

解决方案

The best way to avoid this problem nowadays is by using a DialogFragment.

Create a new class which extends DialogFragment. Override onCreateDialog and return your old Dialog or an AlertDialog.

Then you can show it with DialogFragment.show(fragmentManager, tag).

Here's an example with a Listener:

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中屏幕旋转防止对话框解除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆