Search code examples
androidprogress-barandroid-ion

How to prevent a ProgressBar from sticking?


In my app I'm getting some TEXT from a Website by ion library and I'm using a progressBar to show the progress of downloading that data.

But my main issue is that in onComplete I implemented some methods that work with the TEXT I've got from the website these are some cyclic methdods that use lot's of for and other stuff and actually my progressBar at the beginning works properly, but when it's complete downloading the data the progressBar just sticks until all the methods are complete in onComplete().

I would that the progressBar runs anyway and doesn't get stuck without removing methods from onComplete() is it possible?

Here is my function where I use ion library that I invoke in the onClick() event:

private void getHTML(){
    progressDialog = new SpotsDialog(MainActivity.this, R.style.Custom);
    progressDialog.show();
    Ion.with(getApplicationContext())
            .load("IP")
            .asString()
            .setCallback(new FutureCallback<String>() {
                @SuppressLint("SetTextI18n")
                @Override
                public void onCompleted(Exception e, String result) {
                    htmlresultart = result;
                    htmlresultart = htmlresultart.replace("</td>", "\n");
                    getTable();
                    getHeader();
                    getBody();
                    progressDialog.cancel();
                }
            });

}

Solution

  • If the methods called in the onCompleted() callback take too much to execute then they need to be run on a background thread. In this situation it doesn't make sense to use a callback with Ion, instead you should synchronously fetch the data and after this do all the other tasks, all on a single background thread, something like below:

    private void getHTML(){
        progressDialog = new SpotsDialog(MainActivity.this, R.style.Custom);
        progressDialog.show();
        // start a background thread
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(new Runnable() {
             @Override
             public void run() {
                String htmlresultart = null;
                try {  
                     String htmlresultart = Ion.with(getApplicationContext())
                       .load("IP")
                       .asString()
                       .get(); 
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                // if htmlresultart is null at this point, an exception occured and the string couldn't be fetched so you most likely should abort the following processing.
                if (htmlresultart != null) {
                    htmlresultart = htmlresultart.replace("</td>", "\n");
                    getTable();
                    getHeader();
                    getBody();
                    progressDialog.cancel();
                }
             }
        });        
    }