0

I turn circular indeterminate progressBar to visible and then I run Thread, that can take few seconds (I tried AsyncTask, but it didn't work, probably because there is another AsyncTask running inside the method). However, the progressBar gets visible only just after the Thread is completed. How to make it visible before running the thread, so that the user can see, that something is happening?

Edit: The code is quite complicated, so it is not possible to attach it all without long description, however, I will try to attach important parts of the code

  //this is the Thread class
  private static final class LoadingMolecules extends Thread{
        private Intent intent;
        private ArrayList<Integer> groups;
        Context context;
        QuizProvider quizProvider;

    public LoadingMolecules (Intent intent,ArrayList<Integer> groups, Context context ){
        this.intent = intent;
        this.groups = groups;
        this.context = context;
    }

    @Override
    public void run() {
        quizProvider = new QuizProvider(groups, context);
        //inside next method is the asyncTask
        quizProvider.loadAllCompounds();
    }


}


//there is the code running
progressBar.setVisibility(View.VISIBLE);
        LoadingMolecules lm = new LoadingMolecules(intent, groups, this);
        lm.start();
        try {
            lm.join();
        }catch (InterruptedException ignore){

        }
        intent.putExtra("quizProvider", lm.quizProvider);
        startActivity(intent);
        finish();
Adam Knirsch
  • 443
  • 1
  • 6
  • 16

1 Answers1

0

you should:

  1. visible the progress.
  2. start background thread.
  3. hide the progress in ui thread.

such that:

progress.visibility = View.VISIBLE
AppController.getInstance().getAppExecutors().diskIO().execute(() -> {
   //do stuff
    runOnUiThread(() -> {
         //do stuff
         progress.visibility = View.GONE
    });

});