Why isn't it a good idea to directly instantiate the Dialog class?
I want to show a dialog when the user clicks on the options menu inside the Activity. I first wanted to do this using the Dialog class. The code is similar to the one below.
final Dialog d = new Dialog(this);
d.setContentView(R.layout.customDialog);
d.setTitle("Sample title");
data = (EditText) d.findViewById(R.id.data);
button = (Button) d.findViewById(R.id.aButton);
d.show();
button.setOnClickListner(new View.OnClickListner() {
// grab data from edittext and save it to some var
d.dismiss();
});
Something like that. The dev manual suggests that I do not instantiate the Dialog class directly. Is there anything particularly bad about this approach?
+3
source to share
2 answers
The system stores a cache of dialogs, so you don't need to manage it yourself. Each dialog is created only once and stored somewhere so that it can be reused later. This is because creating a dialogue is computationally expensive I think. The system gives you hooks to create a dialogue the first time you need it, and prepare it before showing it
0
source to share