user-44651 user-44651 - 4 months ago 32
Android Question

Java Class calling method in Activity

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.


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

interface CompletionCallback {
  void finish();


class MyActivity ... implements CompletionCallback {

  void doTheThing() {
    MyClass someMyClass = new MyClass();

  void finish() { ...

And finally:

class MyClass {
  void doSomething(CompletionCallback callback) {

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