Thanks, Dianne, and others. This is what I needed to know.

I know I can async preload Drawables and Bitmaps (there is an official
tutorial, too), but it seems that my custom View is so complex that it
takes 1 second to instantiate (even if its drawables are preloaded).
And I need to *instantly* appear when the Activity is displayed that
uses it. This is the reason I preinstantiate my View to a static (yes,
I take care to null it out, so there will be no leak).

Assume I have Activity A and B. Is the below solution allowed then
(all in UI thread, just at different times):

1. From Activity A: sStaticVar = new
MyCustomView(this.getApplicationContext())

2. From Activity B onCreate():
mRootLayout.addChild(sStaticVar);   // instead of "addChild(new
MyCustomView(this))" or "new MyCustomView(getApplicationContext())"

In this way, each time Activity B is must be displayed, it will
display instantly. My SystemClock measurements seem to confirm this.

On Jun 27, 12:23 am, Dianne Hackborn <[email protected]> wrote:
> No, it is not safe.  Don't do this.
>
> The documentation is very clear about 
> this:http://developer.android.com/reference/android/view/View.html
>
> "Note: The entire view tree is single threaded. You must always be on the
> UI thread when calling any method on any view."
>
> This wasn't written to mislead people into thinking that it works a
> different way than it does: it is because it is true, the view hierarchy is
> single-threaded, and it is explicitly not designed to be used from multiple
> threads.
>
> If you think that not seeing checks about which thread calls are being made
> on indicates that something is safe to use from multiple threads, you
> should probably stop whatever multi-threaded programming you are doing and
> rethink it. :}
>
> Even instantiating a view from one thread and attaching it to a view
> hierarchy in another is not safe, because many views will internally create
> Handler objects that are bound to whatever thread they were created on,
> causing you to have parts of your view hierarchy running on the wrong
> thread with bad results.
>
> On Tue, Jun 26, 2012 at 2:00 PM, Tamás Kovács
> <[email protected]>wrote:
>
>
>
>
>
>
>
>
>
> > It works seamlessly like a charm. It would be nice to know if this is
> > supported/acceptable generally.
>
> > I hope we'll get an answer from the Android guys/ladies.
>
> > On Jun 26, 10:53 pm, Justin Anderson <[email protected]> wrote:
> > > I'm not sure, but I doubt it...  Have you tried it? Did it work?
>
> > > Thanks,
> > > Justin Anderson
> > > MagouyaWare Developerhttp://sites.google.com/site/magouyaware
>
> > > On Tue, Jun 26, 2012 at 2:27 PM, Tamás Kovács
> > > <[email protected]>wrote:
>
> > > > I know that the UI elements (View hierarchy) may only be manipulated
> > > > from the UI thread. For a background operation, the AsyncTask can be
> > > > used, which offers event handers to reach the UI thread.
>
> > > > To be brief, is it allowed to instantiate a View (tied to
> > > > getApplicationContext()) in a non-UI thread? This custom View
> > > > descendant -- once instantiated -- is added to the view hierarchy from
> > > > the UI thread. So only the constructor call is done inside an
> > > > Asynctask.doInBackground; its attaching (addView(...))to the
> > > > Activity's root layout hierarchy is still done in the UI thread.
>
> > > > (I pre-instantiate the View in an asynctask because when it's needed
> > > > in an Activity, it must be instantly displayed.)
>
> > > > --
> > > > 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
>
> > --
> > 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