Note that there are some explanatory texts on larger screens.

plurals
  1. PODialogFragment - setting initial values and retaining state after rotation
    primarykey
    data
    text
    <p>I have created an <em>DialogFragment</em> which is building and returning <em>AlertDialog</em> from <em>onCreateDialog</em> method. The AlertDialog contains two EditText views. </p> <p>I'm setting the initial values of these two edit texts in the <em>onCreateDialog</em> method which works great until I rotate the phone and all changes get lost/restored to initial values because the <em>onCreateDialog</em> is recalled. </p> <p>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?</p> <p>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. </p> <p>What is best practice?</p> <pre><code>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 = &lt;get the object from database&gt;; 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 </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload