Reset an Android Dialog

点点圈 提交于 2020-01-05 08:09:39

问题


I found plenty of topics on how to save states of a dialog, for instance using Bundle instances. However, I can't seem to find how to "properly" reset a dialog.

Consider this sample custom dialog (the XML layout carries an EditText with ID "input_text"):

public class CustomDialog extends Dialog {
  public CustomDialog (Context context) { super (context); }

  protected onCreate (Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);

    setContentView(R.layout.input_query);
    EditText txt = (EditText) findViewById(R.id.input_text);

    // Consider that I'm declaring here the use of listeners
    // in order to retrieve input text.
  }
}

I'm popping up this dialog when a button is clicked in the main Activity. The second time this action occurs, my EditText contains the input from the previous instance.

Now, I know I can reset the content of this EditText, but I'm wondering if there is a common, already existant method to do this, dialog-wide. For instance if I have a more complex dialog, I want that it has "default" values each time this object is instantiated. I thought removing the call tosuper.onCreate(savedInstanceState); could do the trick, but it does not.

Sorry if this is a silly question (or if already addressed... I did not find it)... Any help is appreciated. Thanks a lot!


回答1:


Normally you should override onCreateDialog and onPrepareDialog methods of the activity.

For the first time creation of dialogs, onCreateDialog is called, so you should create your dialog in this method. To initialize/update the contents of dialogs override onPrepareDialog, it is always called before showing a dialog.




回答2:


Try using method opposite to showDialog() - removeDialog() is that method. I was having problem similar to yours. Every dialog i was showing was having data from the first instance. When I started to use removeDialog() the content was being updated properly.



来源:https://stackoverflow.com/questions/7482448/reset-an-android-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!