0

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

1
  • 1
    Can you elaborate on what you're trying to accomplish? Typically "instance variables" are members of the class and should be defined at that scope (the position in the file, i.e. top, doesn't matter). Commented Sep 10, 2012 at 19:24

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.