Anything you can write as static initializers should be safe.  You can't do
anything that requires the Context or Activity be initialized until
onCreate().

On Thu, Apr 21, 2011 at 11:45 AM, RLScott <[email protected]> wrote:

> It seems that many actions can be performed in either the onCreate()
> method for an activity or in the variable initializer section at the
> beginning of the class definition.  For example,  I could write:
>
>     public class Main extends Activity {
>        private Handler handler = new Handler();
>        . . .
>
> or I could write:
>
>     public class Main extends Activity {
>        private Handler handler;
>
>     public void onCreate(Bundle savedInstanceState)
>     {
>         super.onCreate(savedInstanceState);
>         handler = new Handler();
>         . . .
>
> What are the considerations that would make me want to do the
> initialization in one place rather than the other?  Both forms of
> initialization happen exactly once whenever the class is instantiated,
> right?  And the variable initializers at the top happen before the
> code in onCreate() is run.  But are there any other considerations as
> to where I should initialize values for variables?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to