Re: [android-developers] Re: Drawing views in an AsyncTask

2010-11-07 Thread Romain Guy
It doesn't work because you are blocking the UI thread. You are not
"streaming" the UI at all like I suggested you do. You need to show the
dialog, then build the views by little batches so that you never block the
UI thread for more than a few milliseconds at a time. Otherwise your app
will appear frozen and might even get an ANR if the CPU is busy with another
task,

On Sun, Nov 7, 2010 at 9:49 AM, Bret Foreman  wrote:

> Following Romain's suggestion, I changed the code above to look as
> below. The interesting thing to note is that the behavior remains
> exactly the same. The dialog gets dismissed when the activity is new
> but won't dismiss when there is a configuration change. According to
> my understanding, the runnable is running on the UI thread so the
> thread-safety of the framework should not be an issue. Is this a
> framework bug?
>
> public class MyActivityClass {
>
>@Override
>protected void onCreate(Bundle savedInstanceState) {
>super.onCreate(savedInstanceState);
>setContentView( R.layout.showtableview );
>doBuild(savedInstanceState);
>}
>
>Bundle SIS;
>
>protected void doBuild( Bundle savedInstanceState ){
>showDialog(drawingProgressDialogId);
>ScreenBuildRunnable sbRunnable = new ScreenBuildRunnable();
>Handler screenBuildHandler = new Handler();
>SIS = savedInstanceState;
>screenBuildHandler.postDelayed(sbRunnable, 100); // Wait 100
> mS
> for progress dialog to get drawn
>}
>
>private class ScreenBuildRunnable implements Runnable {
>
>@Override
>public void run() {
>LinearLayout topLevelLayout = new
> LinearLayout(SpreadsheetActivity.this);
>doDraw( SIS , topLevelLayout );
>dismissDialog( drawingProgressDialogId );
>setContentView(topLevelLayout);
> }
>}
> }
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Re: Drawing views in an AsyncTask

2010-11-06 Thread Mark Murphy
On Sat, Nov 6, 2010 at 4:08 PM, Romain Guy  wrote:
> Have you thought about using a ListView instead btw?

Agreed. Or any other AdapterView. Or writing your own AdapterView.
Anything to reduce your heap and stack consumption.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9
Available!

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


Re: [android-developers] Re: Drawing views in an AsyncTask

2010-11-06 Thread Kumar Bibek
Well, if you were to show 1000s views on the screen, you would obviously
need more memory. Try creating a list view (a dumb one which returns a new
view every-time) and see if the amount of memory is substantial.

Delay is fine, but I cannot think of a situation where someone might have to
bring up 1000s of views on a screen.


On Sun, Nov 7, 2010 at 1:35 AM, Bret Foreman  wrote:

> In what way is it too much? I'm not using much memory and the delay is
> only a few seconds. It will all be fine if I can figure out how to
> present the user a progress dialog or other indication that work is in
> progress.
>
> On Nov 6, 12:58 pm, Kumar Bibek  wrote:
> > 1000's of views I guessThats too much...
> >
> >
> >
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Kumar Bibek
http://techdroid.kbeanie.com
http://www.kbeanie.com

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

Re: [android-developers] Re: Drawing views in an AsyncTask

2010-11-06 Thread Romain Guy
You can display a progress bar or even a progress dialog and still stream
the UI. Have you thought about using a ListView instead btw?

On Sat, Nov 6, 2010 at 1:05 PM, Bret Foreman  wrote:

> In what way is it too much? I'm not using much memory and the delay is
> only a few seconds. It will all be fine if I can figure out how to
> present the user a progress dialog or other indication that work is in
> progress.
>
> On Nov 6, 12:58 pm, Kumar Bibek  wrote:
> > 1000's of views I guessThats too much...
> >
> >
> >
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Re: Drawing views in an AsyncTask

2010-11-06 Thread Kumar Bibek
1000's of views I guessThats too much...

On Sun, Nov 7, 2010 at 1:27 AM, Bret Foreman  wrote:

> My Moto Droid builds the Views in a few seconds and everything else
> works great - scrolling is fast and pretty, memory utilization is
> modest. The _only_ problem is how to handle the brief delay while the
> Views are built. I'm not sure what you mean by sensible.
>
> On Nov 6, 12:44 pm, Mark Murphy  wrote:
> > On Sat, Nov 6, 2010 at 3:29 PM, Bret Foreman 
> wrote:
> > > A few hundred Views in a typical case but it could be up to a couple
> > > thousand in some cases.
> >
> > And what makes you think that "a couple thousand" is sensible, on any
> platform?
> >
> > --
> > Mark Murphy (a Commons Guy)http://commonsware.com|
> http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
> >
> > _The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9
> > Available!
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en




-- 
Kumar Bibek
http://techdroid.kbeanie.com
http://www.kbeanie.com

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

Re: [android-developers] Re: Drawing views in an AsyncTask

2010-11-06 Thread Mark Murphy
On Sat, Nov 6, 2010 at 3:29 PM, Bret Foreman  wrote:
> A few hundred Views in a typical case but it could be up to a couple
> thousand in some cases.

And what makes you think that "a couple thousand" is sensible, on any platform?

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9
Available!

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


Re: [android-developers] Re: Drawing views in an AsyncTask

2010-11-06 Thread Mark Murphy
On Sat, Nov 6, 2010 at 3:10 PM, Bret Foreman  wrote:
> I've used Traceview extensively on this code - it used to take 20
> seconds and now it's down to 3, all of which is in the View
> constructors and the code where I set various drawing parameters.

Most Views take next to no time to set up. How many Views are in this GUI?

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9
Available!

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


Re: [android-developers] Re: Drawing views in an AsyncTask

2010-11-06 Thread Mark Murphy
On Sat, Nov 6, 2010 at 3:04 PM, Bret Foreman  wrote:
> Streaming the UI creates an added complication - in what way should I
> notify the user that the UI is finished drawing? Partial data is going
> to confuse the user unless they know that more is coming. Since this
> is a limitation imposed by the Android framework, others must have
> encountered it. Is there a standard way to notify the user that the UI
> is incomplete?

Use Traceview and find where your performance issue lies. Then fix
that problem. That way, you won't have to worry about all this other
stuff, and you'll have happier users as an extra bonus.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9
Available!

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


Re: [android-developers] Re: Drawing views in an AsyncTask

2010-11-06 Thread Mark Murphy
On Sat, Nov 6, 2010 at 2:51 PM, Bret Foreman  wrote:
> The time
> taken is just in building the view hierarchy itself - creating and
> initializing the Views takes a few seconds.

Use Traceview and find where your performance issue lies. "Building
the view hierarchy itself" is a very broad statement.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9
Available!

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


Re: [android-developers] Re: Drawing views in an AsyncTask

2010-11-06 Thread Romain Guy
She did not suggest you crated the Views themselves in background thread,
but that your computations were done in a background thread. You need to
split the creation of the Views and whatever you are doing that's taking a
long time to compute. You can also stream the UI by adding Views one after
the other using Handler./View.post(Runnable).

On Sat, Nov 6, 2010 at 11:24 AM, Bret Foreman wrote:

> What is the best way to handle the case where drawing takes a few
> seconds? I need to put up some sort of progress dialog while I work on
> building the screen.
>
> On Nov 6, 11:18 am, Romain Guy  wrote:
> > You should NEVER create or draw Views from a background thread. The UI
> > toolkit (and the framework in general) is not thread safe.
> >
> > On Sat, Nov 6, 2010 at 10:21 AM, Bret Foreman  >wrote:
> >
> >
> >
> > > It takes some time to draw my views so I want to put up a progress
> > > dialog while I do it. I set up the code like this:
> >
> > > public class MyActivityClass {
> >
> > >@Override
> > >  protected void onCreate(Bundle savedInstanceState) {
> > >  super.onCreate(savedInstanceState);
> > >  setContentView( R.layout.myView );
> > >  doBuild(savedInstanceState);
> > >  }
> >
> > >protected void doBuild( Bundle savedInstanceState ){
> > >showDialog(drawingProgressDialogId);
> > >ScreenBuilder builder = new ScreenBuilder();
> > >builder.execute(savedInstanceState);
> > >}
> >
> > >private class ScreenBuilder extends AsyncTask > > LinearLayout > {
> >
> > >@Override
> > >protected LinearLayout doInBackground(Bundle... SIS) {
> > >LinearLayout topLevelLayout = new
> > > LinearLayout(MyActivityClass.this);
> > >doDraw( SIS[0] , topLevelLayout ); // Adds a bunch
> of
> > > child views
> > >return topLevelLayout;
> > >}
> > >@Override
> > >protected void onPostExecute(LinearLayout
> topLevelLayout) {
> > >dismissDialog(drawingProgressDialogId);
> > >setContentView(topLevelLayout);
> > >super.onPostExecute(topLevelLayout);
> > >}
> > >}
> > > }
> >
> > > This works the first time through, where savedInstanceState is null.
> > > But when there is a configuration change, like rotation and
> > > savedInstanceState is not null, the dismissDialog call does not work.
> > > The progress dialog remains on the screen. This is true even if doDraw
> > > is stubbed so that I'm not doing anything with the savedInstanceState.
> >
> > > Any ideas why this behavior might occur?
> >
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "Android Developers" group.
> > > To post to this group, send email to
> android-developers@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > android-developers+unsubscr...@googlegroups.com
> 
> >
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-developers?hl=en
> >
> > --
> > Romain Guy
> > Android framework engineer
> > romain...@android.com
> >
> > Note: please don't send private questions to me, as I don't have time to
> > provide private support.  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 android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en