[android-developers] Re: Why does not this sleep work?
Thank you for your help. It's great to learn some fundamental
concepts.
This is not just my first Android app, this is my first Java
code.
On Jul 2, 11:35 pm, Bob Kerns <r...@acm.org> wrote:
> First, don't rush to learn about threads and threading. There's a lot
> to learn, it's subtle, mistakes are easy to make but hard to detect
> and analyze.
>
> That's why so many frameworks, including Android, go out of their way
> to make directly working with threads unnecessary for most people and
> purposes.
>
> The mistake you are making here is not in your code, so much as it's
> your expectations.
>
> You expect that when you set the background color, that something
> happens immediately. That's not true.
>
> You are just recording what to use. Then you wait, and then you set it
> back.
>
> Then, and only then, you return -- and then, and only then, the system
> calls the methods that update the actual screen. That update does not
> happen until you are done with your onClick() method, because you
> don't want to see partial results.
>
> So all you've done by calling Thread.sleep() is to delay updating the
> screen.
>
> A good rule of thumb in Android is that if you're calling
> Thread.sleep(), you're doing it wrong.
>
> What you want to do instead, is to put your call
> textResult.setBackgroundColor(Color.BLACK); where it is, and put the
> call textResult.setBackgroundColor(Color.WHITE); in some code to run
> later, after 500 ms.
>
> You can do this with a Handler object and a Runnable, like this:
>
> private Handler myHandler = new Handler();
>
> protected void showNewDraw() {
> textResult.setBackgroundColor(Color.BLACK);
> textResult.setText("Before sleep");
> myHandler.postDelayed(500, new Runnable() {
> public void run() {
> textResult.setBackgroundColor(Color.WHITE);
> textResult.setText("After sleep");
> }
> }
>
> This will set the background to BLACK, and request that it run the
> other code 500 ms later, and return. The screen will then be updated
> with the new information, and 500 ms later, the Runnable will be
> called and perform the change.
>
> By the way, this is also the case in most modern UI toolkits, not just
> android. You should never do ANYTHING in your UI routines that takes a
> long time, and especially not deliberately wait. There are things in
> Android that are unique, but this is not one of them. So what you
> learn here you can apply elsewhere as well.
>
> On Jul 2, 1:13 pm, Dmitri Snytkine <d.snytk...@gmail.com> wrote:
>
> > Hello!
> > I am new to android development, also new to java. I have many years
> > of experience with OO PHP, so it's not hard for me to quickly learn
> > Java and Android, but I still don't know many things like Thread
> > class.
>
> > I just started developing a simple app.
>
> > What I want to do is that when a button is clicked, then the TextView
> > is update in this way:
> > the background is changed to black (by default it's set to white)
>
> > Then it should wait a half a second and then set the background back
> > to white and update the text inside the TextView
>
> > Here is my code, it's inside my Activity class:
>
> > public synchronized void onClick(View v) {
> > // TODO Auto-generated method stub
> > switch (v.getId()) {
> > case R.id.btnPlay:
> > showNewDraw();
> > break;
> > }
>
> > }
>
> > protected void showNewDraw() {
> > textResult.setBackgroundColor(Color.BLACK);
> > textResult.setText("Before sleep");
>
> > try {
> > Thread.sleep(500);
> > } catch (Exception e) {
> > textResult.setText("Something wrong");
> > }
>
> > textResult.setBackgroundColor(Color.WHITE);
> > textResult.setText("After sleep");
> > }
>
> > The way it works now is that it seems to actually take 1/2 second from
> > the time the button is pressed and text is updated, but the background
> > is not reset to black and back to white - it always stays white.
>
> > Either it is being set to black and instantly reset to white or it's
> > just never set to black.
>
> > What should I do in order for the "delay" effect to work?
>
>
--
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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home