1

I've got a Thread parsing XML in the background of my app. As it does so, it updates a progress bar in the activity's view via a Handler. This works fine until the phone changes orientation (and probably during other destructive actions, like multitasking, though I haven't throughly tested there). After any number of rotations, the progress bar freezes where it is and never progresses, even when the parsing is done and my ListView updates itself just fine.

Here's a cut-down version of the relevant code. I'm not including it here, but I do have code in place to make sure the thread is continuing unencumbered after the rotation--like I said, the parsing eventually finishes and updates the ListView in the same amount of time. It's just the progress bar handler that doesn't work:

private ProgressBar mProgress;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.directory_list);
    mProgress = (ProgressBar) findViewById(R.id.progressBar);
    mProgress.setMax(entryCount);
    startXMLParseThread();
}

private void startXMLParseThread() {
     new Thread () {
           Handler hUpdateProgressBar = new Handler(){
                    public void handleMessage(Message msg) {
            mProgress.setProgress(entryCount);
                    }
           };

           public void run() {
                    while (parserEvent != XmlPullParser.END_DOCUMENT) {
                            entryCount++;
                            hUpdateProgressBar.sendEmptyMessage(0);
                            parserEvent = parser.next();
                    }

                    mHandler.post(new Runnable() {
                            public void run() {
                                      adapter.getCursor().requery();
                            }
               });
           }
     }.start();     
}
LouieGeetoo
  • 868
  • 2
  • 11
  • 26
  • see http://stackoverflow.com/questions/1111980/how-to-handle-screen-orientation-change-when-progress-dialog-and-background-threa and http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working – bigstones Mar 21 '11 at 20:12

1 Answers1

1

May be its running the onCreate method again on orientation change ...so it loses reference to previous handler...try this ..add attribute to the manifest file in that particular activity android:configChanges="orientation"...in this way it wont run onCreate again...give it a shot...

user484691
  • 381
  • 7
  • 19
  • Thanks for the suggestion. I'd already tried it, but I tried it again, and unfortunately nothing changes. The bar still gets stuck. EDIT: Ah, wait! I was putting it in the wrong place in the manifest. After putting it under `activity`, it now works like I want it to. Thank you! I hope I don't run into trouble for not doing things the "Android way", though. Doesn't Google recommend recreating the View orientation changes? Or is that only for if you use different layouts for the different orientations? – LouieGeetoo Mar 21 '11 at 20:53
  • 1
    Yes they do. You should only use this method if you know for sure you are handling orientation changes properly. A different way would be to pass yourself a reference to the `Thread` in `onRetainNonConfigurationInstance` and then get the reference in `onCreate` using `getLastNonConfigurationInstance` and check it is not null (in which case an existing task will be running so don't start one). You do want to set the handler everytime `onCreate` is called though so add a `setHandler` method to your thread and replace the existing handler. – Joseph Earl Mar 21 '11 at 21:26
  • It seems to be working as-is, but if I run into any trouble I'll come back and try what you've suggested. Thanks very much for the informative comment! – LouieGeetoo Mar 23 '11 at 02:51