DialogFragment - 设置初始值和旋转后保持状态 [英] DialogFragment - setting initial values and retaining state after rotation

查看:339
本文介绍了DialogFragment - 设置初始值和旋转后保持状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个的 DialogFragment 的正在建设和 onCreateDialog 的返回的 AlertDialog 的方法。该AlertDialog包含两个EditText上的看法。

I have created an DialogFragment which is building and returning AlertDialog from onCreateDialog method. The AlertDialog contains two EditText views.

我设置这两个编辑文本的初始值在 onCreateDialog 的方法,直到我转动电话和所有更改会丢失/恢复到初始值伟大的工程,因为 onCreateDialog 的被调出。

I'm setting the initial values of these two edit texts in the onCreateDialog method which works great until I rotate the phone and all changes get lost/restored to initial values because the onCreateDialog is recalled.

我的问题是我应该在哪里放置初始值,因此它们只设置你第一次打开对话框,如果你做了改变,你旋转你的电话,我保留,retached最后状态?

So my question is where should I place the initial values so they are only set the very first time you open the dialog and if you have done changes and you rotate your phone, the last state i retained and retached?

下面我贴我的code的简化版本。一种解决方案可以在可以的newInstance初始化类属性()方法,但我需要让他们静。其他的解决办法可以通过捆绑路过的值,但没有放的方法采取日历作为参数类型。

Below I have pasted simplified version of my code. One solution could be initializing the class attributes at newInstance() method, but then I need to make them static. Other solution could be passing the values through the Bundle, but no put-methods take Calendar as parameter type.

什么是最好的做法?

public class MyDialogFragment extends DialogFragment implements OnClickListener, OnDateSetListener, OnQuantitySetListener 
{
private EditText editText1, editText2
private MyObject myObject;


public static MyDialogFragment newInstance() 
{
    return new MyDialogFragment ();
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater factory = LayoutInflater.from(getActivity());
    final View v = factory.inflate(R.layout.my_layout, null);

    editText1 = (EditText) v.findViewById(R.id.text1);
    editText2 = (EditText) v.findViewById(R.id.text2);

    myObject = <get the object from database>;

    editText1.setText(myObject.attribute1);
    editText2.setText(myObject.attribute2);


    bindDataToViews();

    return new AlertDialog.Builder(getActivity())
            .setIconAttribute(R.drawable.add)
            .setTitle("Title of the dialog")
            .setView(v)).create();
}

... other methods using getting the values from EditText and putting them back to MyObject

}

推荐答案

您可以在存储的onSaveInstanceState(捆绑outState)方法中的数据,并在onRestoreInstanceState()方法再次阅读。会的onSaveInstanceState屏幕旋转之前onRestoreInstanceState()更改后调用,。这是方向改变的存储数据的好地方。

You can store your data in onSaveInstanceState(Bundle outState) method, and read them again in onRestoreInstanceState() method. onSaveInstanceState will be called before screen rotation, and onRestoreInstanceState() after change. This is a good place to store data between orientation changes.

或者你也可以加在清单文件

Or you can also add in Manifest file

android:configChanges="orientation"

添加此值activitiy,其中包含您alertDialog。

Add this value to activitiy, which contains your alertDialog.

这篇关于DialogFragment - 设置初始值和旋转后保持状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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