Ayub
Ayub

Reputation: 510

Bolts Framework: onSuccess make changes on UI/Main thread

Is it possible to display a message or make changes on the UI thread once Task callInBackground is finished executing?

Something like following:

Task.callInBackground(new Callable<String>() {
            @Override
            public String call() {


                for(int i=0; i<3; i++){
                    Log.i("I=", String.valueOf(i));

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                String obj = "";
                return null;
            }
        }).onSuccess(new Continuation<String, Object>() {
            @Override
            public Object then(Task<String> task) throws Exception {
                Log.i("I=", "Counter complete");

                Toast.makeText(MainLoanMemberActivity.this, "Finished", Toast.LENGTH_SHORT).show();
                btnAgriLoan.setText("LOL");
                return null;
            }
        });

At the moment there is no Toast message displayed and there is no crash as well.

Looking for an equivalent of AsyncTask's onPostExecute in Bolts Framework where one can add changes to UI.

Upvotes: 1

Views: 169

Answers (1)

Ayub
Ayub

Reputation: 510

Didn't realized that there are EXECUTOR's types that you can mentioned with each helper function like so: (Task.UI_THREAD_EXECUTOR)

Task.callInBackground(new Callable<String>() {
            @Override
            public String call() {
                for(int i=0; i<3; i++){
                    Log.i("I=", String.valueOf(i));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                String obj = "";
                return null;
            }
        }).onSuccess(new Continuation<String, Void>() {
            public Void then(Task<String> object) throws Exception {
                Toast.makeText(MainLoanMemberActivity.this, "Finished", Toast.LENGTH_SHORT).show();
                btnAgriLoan.setText("LOL");
                return null;
            }
        }, Task.UI_THREAD_EXECUTOR);

Docs helped!

Upvotes: 0

Related Questions