0

The following code is from Beginning Android 3, Chapter 20. When the phone is rotated, a new activity will be created and onStart() will be called, and so bar.setProgress(0) is called. However, I don't see the bar's progress is back to the beginning. Why not?

public class HandlerDemo extends Activity {
    ProgressBar bar;
    Handler handler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            bar.incrementProgressBy(5);
        }
    };

    AtomicBoolean isRunning=new AtomicBoolean(false);

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        bar=(ProgressBar)findViewById(R.id.progress);
    }
    public void onStart() {
        super.onStart();
        bar.setProgress(0);
        Thread background=new Thread(new Runnable() {
            public void run() {
                try {
                    for (int i=0;i<20 && isRunning.get();i++) {
                        Thread.sleep(1000);
                        handler.sendMessage(handler.obtainMessage());
                    }
                } catch (Throwable t) {
                // just end the background thread
                }
            }
        });
        isRunning.set(true);
        background.start();
    }

    public void onStop() {
        super.onStop();
        isRunning.set(false);
    }
}
user1443721
  • 1,110
  • 2
  • 14
  • 33
  • read this :) http://stackoverflow.com/questions/4348032/android-progressbar-does-not-update-progress-view-drawable – lv0gun9 Dec 03 '14 at 05:46

1 Answers1

1

Try using this code

@Override
   protected void onPause() {
           super.onPause();
           isRunning.set(false);
           bar.setProgress(0);
   }
BalaChandra
  • 632
  • 9
  • 33