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.setTitle("Sample title");

data = (EditText) d.findViewById(R.id.data);
button = (Button) d.findViewById(R.id.aButton);

button.setOnClickListner(new View.OnClickListner() { 
    // grab data from edittext and save it to some var 



Something like that. The dev manual suggests that I do not instantiate the Dialog class directly. Is there anything particularly bad about this approach?


source to share

2 answers

The Android developer guide contains a lot of additional information to help developers avoid tasks that take a lot of processing time. Instantiating the Dialog class directly is likely to take a lot more processing time.



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



All Articles