Search code examples
javaandroidperformanceprocessing-efficiencymemory-efficient

Should I create Edittexts when custom dialogBox is created or before onCreate? Which is more efficient?


I have an Android app in which I create a custom dialogBox which is built from an XML file. The custom dialogBox contains 10 different edittext box's. My question is, in my MainActivity.java code, should I create these edittext's when my code creates the custom dialogBox or should I just create them before onCreate? Which one is more efficient/best pratice? Does each have it's own uses?

For example:
Here

EditText et1;

        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);...


Or here

public void dialogSettings() {

            final Dialog dialogBox = new Dialog(context);
            dialogBox.setContentView(R.layout.custom_dialog_settings);
            dialogBox.setTitle(R.string.action_settings);

            EditText et1;

            dialogBox.show();

        }

Solution

  • It all depends where you want to use it. If you are using it inside onCreate only then declare it inside onCreate otherwise define it globally.