2

Is it better to give default values to your instance variables in onCreate, onResume or just when declaring them? In code:

When declaring them:

public Class Foo extends Activity{

    private String variable1 = "my super var";
    private int variable2 = 42;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

OR during onCreate

public class Foo extends Activity{

    private String variable1;
    private int variable2;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        variable1 = "my super var";
        variable2 = 42;
    }
}

OR during on resume:

public class Foo extends Activity{

    private String variable1;
    private int variable2;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected void onResume() {
        super.onResume();
        variable1 = "my super var";
        variable2 = 42;
    }   
}

Thank you.

1
  • That depends of your app lifecycle..., but maybe onResume is the choice, in my case, i use onCreate to set listeners and every visual stuff
    – DGomez
    Apr 12, 2012 at 18:50

3 Answers 3

3

The above answers are good, and it does depend on coding style BUT, as a rule, try to avoid assigning default values other than when the variables are declared. This will help with maintainability. When you come back to your code in 6 months to fix a weird bug, having defaults scattered throughout the code will add to your troubles.

Only use onCreate, onResume etc if there is a purpose in doing so and then, add a meaningful comment e.g.

// re-initialise foobarValue when the app regains focus
// to ensure that the wobbly gong generator starts from the beginning
foobarValue = 1;

Cheers

1
  • 1
    Would also add to this the liberal use of static final values for use for one's initialized defaults as a way of aggregating all of your defaults into one place.
    – JRaymond
    Apr 12, 2012 at 19:47
2

That all depends on your sense of coding style, and of course the instance variables being assigned. Often, a fair number of my instance variables are views and thus depend on having a valid context - so these I of course put in onCreate(). If I need a variable to be reset every time I leave an activity and return, then it has to be in onResume(). As for inline, I tend to do this when I know that the variable will not change and shouldn't change, and I make them final. The balance between inline constructed variables and onCreate() depends. Some people might like to have all of their assignments in one place, so onCreate() makes sense as a way to capture all assignments in a single location, if you don't care than there's no real performance difference between them, so put the assignment where you want.

0

I don't think there's a whole lot of difference from doing it as default values or in onCreate if the values are simple. What onCreate is good for is if you need activity-related things like a Context or something from the Intent, etc. Then you have to do it in onCreate.

However, doing this initialization in onCreate vs. onResume makes a huge difference. If you put code in onResume it runs every time you regain focus in the activity (for an exact definition you'll want to read the docs). This means that if you press home and then go back to your app, this code will run again. So it really depends on what behavior you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.