Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:25:38+00:00 2026-06-09T14:25:38+00:00

I have created an DialogFragment which is building and returning AlertDialog from onCreateDialog method.

  • 0

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

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.

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?

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.

What is best practice?

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

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:25:40+00:00Added an answer on June 9, 2026 at 2:25 pm

    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"
    

    Add this value to activitiy, which contains your alertDialog.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a module in Magento which takes certain views from customers about
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created two imageViews promatically as shown below: public void createImageViews(Integer count){ ImageView[]
I have created an xPages application which uses a lot of server side javascript
I have created a custom dialog box extending the DialogFragment class. As stated in
I am trying to get the Dialog I have created with an extended DialogFragment
Have created a ATL COM project through which I am inserting Menu Items to
I have created a bar chart which is a improvement of bar graph on
I have created a console application in C# and there is main method (static)
I have created a PHP form, which, upon submission, goes to another PHP page.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.