Reset an Android Dialog
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!
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.
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.
精彩评论