0

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

Zulqurnain Jutt
  • 1,083
  • 3
  • 15
  • 41
  • Any comments you have on [your previous question](http://stackoverflow.com/questions/39055585/retrofit-otto-aa-how-to-do-simple-get-request)? Same comment there: AA doesn't seem to be the problem - the flow of the code execution is. – OneCricketeer Aug 20 '16 at 21:41
  • @cricket_007 after some careful analysis , i think my problem is what i have listed in this question , AA is just helper class – Zulqurnain Jutt Aug 20 '16 at 21:43

1 Answers1

2

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks its nice idea – Zulqurnain Jutt Aug 20 '16 at 21:53
  • Functionally, yes, it is nice, but can get messy very quickly. The part that is the best IMO, is that from the inner callback method, you just call some `finish()` function that is declared at the class level and pass in whatever parameters you need. – OneCricketeer Aug 20 '16 at 21:56