0

I'm not a Java expert and I would like to understand how to pass a function that must be performed with runOnUiThread

Something similar to this

public void runOnActivity(FUNCTION) {
    mActivity.runOnUiThread(
            new Runnable() {
                @Override
                public void run() {
                    FUNCTION();
                }
            }
    );
}

and then

public void reload(){
    runOnActivity(mContext.reload);
}

Or even better, since they should always be called as mContext methods:

public void runOnActivity(METHOD_NAME) {
    mActivity.runOnUiThread(
            new Runnable() {
                @Override
                public void run() {
                    mContext.METHOD_NAME();
                }
            }
    );
}

and then

public void reload(){
    runOnActivity("reload");
}

All functions always return VOID I hope I explained myself.

Thank you

Amedeo
  • 105
  • 1
  • 9

1 Answers1

0

You can include your function in Runnable block like this

Runnable myFunction = () -> System.out.println("some function");

void runOnMain(Runnable runnable) {
  mActivity. runOnUiThread {
    runnable.run();
  }
}

Finally, call your method:

runOnMain(myFunction);

I hope, you will switch to Kotlin soon and perform such actions much easier :)

David
  • 351
  • 3
  • 11