-1

I have a progress bar that I change from View.GONE to View.VISIBLE, just before I call a method writeFile().

Something like this:

onButtonPress(){
    mProgressBar.setVisibility(View.VISIBLE);
    writeFile();
}

The method writeFile() can take a few seconds (lots of data parsing), but requires additional actions taken by the user afterwards, and thus is not performed on a background thread. This is to avoid the user navigating elsewhere within the app, and then being presented with a pop-up, or redirected, for something they thought they were done with.

My problem is that the Progress Bar does not display until the writeFile() method is complete, and then briefly pops up for a split second before the user is redirected elsewhere.

If I comment-out the method call, then the ProgressBar appears immediately and spins indefinitely, so I know the bar is working properly.

How can I ensure the progress bar pops up FIRST, and then the writeFile() method is called after?

Thanks in advance.

Additionally:

I don't have a lot of experience (exactly none!) with asynctasks, and so am not sure if this is the route I need to take. The behaviour I am looking for is the progress bar to pop up when the user presses the button, and remain on the screen until the method is complete. I don't want the user to navigate around the app during this time.

Birrel
  • 4,754
  • 6
  • 38
  • 74
  • Does the writeFile method run in the main thread? If so that is your problem as the main thread will block UI updates. – Thorben Aug 26 '15 at 19:08
  • Yes, it does. So this IS an async issue? – Birrel Aug 26 '15 at 19:09
  • 1
    WriteFile is blocking main thread UI, you should use another thread. – Chris Gomez Aug 26 '15 at 19:11
  • 1
    Yes, any operation that takes a bit longer should be done in another thread so the UI will not be blocked. If you don't have any experience in terms of AsyncTask you might want to look here: http://stackoverflow.com/questions/9671546/asynctask-android-example – Thorben Aug 26 '15 at 19:11
  • 1
    Sounds like your writeFile() method have been locked the mainThread causing block on UI – Renan Nery Aug 26 '15 at 19:12
  • @Thorben that'll about do it! – Birrel Aug 26 '15 at 19:13

1 Answers1

0

You can try the below code to check if the progressBar will be visible

new Thread(new Runnable() {
    public void run() {
        writeFile();
    }
}).start();
Renan Nery
  • 3,475
  • 3
  • 19
  • 23