Q:

Call a method in different activities from a non-activity class

I have 3 classes with the same function. The class H is a controller class, so i have made a setactivity class to get the instance of the activity that called it. Now i have to call the function userleft() of the currently initialized class from the controller class H which is a non Activity. 
The error i am getting is "The method userleft() is undefined for the type Activity" and i know that the exception would come, since the class Activity does not contain a method called userLeft(). But what is the best way to make this work?
See the structure of the app below.
public class A extends Activity {

    void onCreate(Bundle savedInstanceState) {
        H.getInstance().setactivity(this);
    }

    public void userleft() {

    }
}

public class B extends Activity {

    void onCreate(Bundle savedInstanceState) {
        H.getInstance().setactivity(this);
    }

    public void userleft() {

    }
}

public class C extends Activity {

    void onCreate(Bundle savedInstanceState) {
        H.getInstance().setactivity(this);
    }

    public void userleft() {

    }
}

and i have another non activity class
public class H {
    public static H getInstance() {
        if (instance == null) {
            instance = new H();
        }
        return instance;
    }

    public void setActivity(Activity activity) {
        this.activityGame = activity;
    }
    public void randomCallBack(){
        activityGame.userleft(); 
    }
}

Thanks!

A:

You need to redesign your classes like below 
Declaire a base activity class 
public abstract class BaseActivity extends Activity {

    public abstract void userleft();
}

And declare your classes like
public class A extends BaseActivity {

    void onCreate(Bundle savedInstanceState) {
        H.getInstance().setactivity(this);
    }

    @Override
    public void userleft() {

    }
}

And now modify your Non-activity class like
public class H {
    public static H getInstance() {
        if (instance == null) {
            instance = new H();
        }
        return instance;
    }

    public void setActivity(BaseActivity activity) {
        this.activityGame = activity;
    }
    public void randomCallBack(){
        activityGame.userleft(); 
    }
}

Now you will be able to call your method for any subclass activity.

A:

In your randomCallBack() method you have an Activity object, the class Activity does not contain a method called userLeft(), so it is normal that you get such an exception. So, you have two ways of solving this issue:
Either, you cast the activityGame to its corresponding class as:
 public void randomCallBack(){
    if (activityGame instanceof A) {
           ((A)activityGame).userleft(); 
    }
    if (activityGame instanceof B) {
           ((B)activityGame).userleft(); 
    }
    if (activityGame instanceof C) {
           ((C)activityGame).userleft(); 
    }
}

Or, in a better way, you create an Interface, which is implemented by your clasess A, B, and C, and used by H class. Like this:
public interface UserLeft {
  public void userleft();
}

public class A extends Activity implements UserLeft {

void onCreate(Bundle savedInstanceState) {
    H.getInstance().setactivity(this);
}

public void userleft() {

}
}

public class B extends Activity implements UserLeft {

void onCreate(Bundle savedInstanceState) {
    H.getInstance().setactivity(this);
}

public void userleft() {

}
}

public class C extends Activity implements UserLeft {

void onCreate(Bundle savedInstanceState) {
    H.getInstance().setactivity(this);
}

public void userleft() {

}
}

public class H {
UserLeft activity;
public static H getInstance() {
    if (instance == null) {
        instance = new H();
    }
    return instance;
}

public void setActivity(UserLeft activity) {
    this.activityGame = activity;
}
public void randomCallBack(){
    activityGame.userleft(); 
}
}

