1

I am trying to understand how to call a method in my activity after a process has been completed in another class?

Please see my illustrated example. In MyActivity I want to call a method in MyClass called doSomething. When doSomething is completed, I want MyClass to (notify?, interface?, delegate?, observable?, insert "I don't know here") my finished method back in Activity.

enter image description here

I have looked at Interfaces. But I don't understand the point. (You basically promise to implement a method? but... why? You can just implement the method without the interface.)

I tried Observable on MyActivity but i can't extend multiple classes...

Delegates look like interfaces.

I don't know. I am still very much learning and hope someone will help me understand. Most of the tutorials I have seen are generally copied and pasted from each other. I recognize I failing to understand a basic function of Java.

user-44651
  • 3,924
  • 6
  • 41
  • 87
  • 1
    Looks like a subscriber-publisher pattern task. You might look at [this answer](http://stackoverflow.com/questions/34710809/is-that-a-right-way-of-using-interface-callback/34711257#34711257). – Onik Nov 03 '16 at 20:19

1 Answers1

1

In order to that, you have to provide a way to do a callback to your other class. Like:

interface CompletionCallback {
  void finish();
}

Then:

class MyActivity ... implements CompletionCallback {
...

  void doTheThing() {
    MyClass someMyClass = new MyClass();
    someMyClass.doSomething(this);
  }

  @Override 
  void finish() { ...

And finally:

class MyClass {
  void doSomething(CompletionCallback callback) {
    ....
    callback.finish()

In other words:

  1. You create an interface that contains the method(s) you need to callback (using an interface allows you to abstract the concrete implementation of the class that should be called back)
  2. Now you simply pass an object of an class that implements that interface to that code that should call you back
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • OMG thank you! What is this pattern called? Can this pattern be applied between Activities? Or is it best to use Intents? – user-44651 Nov 03 '16 at 20:23
  • Well, "callback" ;-) but that would be associated with "more" stuff (like http://www.javaworld.com/article/2077462/learn-java/java-tip-10--implement-callback-routines-in-java.html ) ... in that sense; I am not even sure if there is a specific name for that. As it is really really basic - if one objects wants to call another object, it needs A) a ref to that object and B) a method to call. My personal hint: step back from Android. Dig through **base java education material** for some time. There is no point in learning Android when you dont have a solid foundation in Java. – GhostCat Nov 03 '16 at 20:27
  • Thank you so much! – user-44651 Nov 03 '16 at 20:28
  • In other words: you better work through stuff like https://docs.oracle.com/javase/tutorial/ **first**. Otherwise your experiments with Android will be very frustrating; and probably end soon, because you will run out of motivation ... as you will be hitting one wall after the other. – GhostCat Nov 03 '16 at 20:28