Run Asynchronous Tasks in Order using AA?

47 Views Asked by At

I am trying to run some methods when a button is clicked and then move to next activity something like this:

Clicked Ok button -> func1() -> func2() -> funky() -> move to next activity

i can't seem to understand what should i do to make this pattern work?

Note: func1 , func2 , funky are asynchronous

I've tried EventBus pattern , but that pattern require 1 extra class to be made of each event i know this simple task can't be that expensive

1

There are 1 best solutions below

2
On BEST ANSWER

Button click - This is asynchronous (in a way; the code inside onClick isn't called until the button clicks). What do you do here? You wait until the button is pressed, and then execute the task func1().

func1() - Same idea. Implement a callback for when the task is completed, then execute func2()

Rinse, repeat.

Psuedocode:

button.setOnClickListener(
    new OnClickListener() { // This is a callback anonymous class
        public void onClick(View v) {  // Think of this as a callback method
            func1(
                new Func1Callback() { // Callback anonymous class
                    public void onFunc1Complete() { // Callback method
                        func2(
                            // Repeat
                        ); 
                    }
            });
        }
});

Obviously, this can be refactored to remove the nesting, which is where the EventBus library is nice.