user949386
user949386

Reputation: 3

Android - Instance Variables

is it appropriate to define instance variables on the top of the class or we need to define them in onResume/onPause of the activity

Upvotes: 0

Views: 496

Answers (2)

dmon
dmon

Reputation: 30168

Depends on the type of variable. There are some things (e.g. Views) that are not available until the layout is initialized. For others (e.g. resources) you need a Context so you'd have to wait until onCreate as well.

If you just want to define an integer or a String, namely, stuff that doesn't depend on the Android framework, go nuts.

Upvotes: 1

soren.qvist
soren.qvist

Reputation: 7416

If you are defining instance variables in your onResume method, then you are effectively re-assigning values to them everytime your Activity is resumed (and thus discarding any previous values). Define your instance variables in onCreate, which is called only once pr. Activity life-time.

Upvotes: 0

Related Questions