DEV Community

Dimitrios Desyllas
Dimitrios Desyllas

Posted on

How I can update the Activity's UI using lambdas for events?

As I ask upon: https://stackoverflow.com/q/67150195/4706711

I have a class that I provide callbacks via lambdas. These lambdas provide callback functionality and update the Activity's UI.

In a nutshell I have the following class:

class MyProcess extends Runnable{

   Callback callback;
   Callback exceptionCallback;   

   public function setCallback(Callback callback){this.callback=callback;}

   public function setExceptionCallback(Callback callback){this.exceptionCallback = callback}

   public void doStuff()
   {
      try{
        // Some Logic there that performs Networking
        callback.call();
      }catch(Exception e){ exceptionCallback.call(e); }
   }

   public void run(){ this.doStuff(); }
}
Enter fullscreen mode Exit fullscreen mode

And in my activity I do the following

 class MyActivity extends extends AppCompatActivity implements View.OnClickListener{
   MyProcess process;
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.my_activity);
       process = new MyProcess();
       process.setExceptionCallback((Exception e)->{                Log.e("MyApp",MyActivity.class+e.getMessage());
});
       Button submit = (Button)findViewById(R.id.connect_btn);
       submit.setOnClickListener(this);
       process.setCallback(()->{submit.setEnabled(true)});
  }
  @Override
  public void onClick(View v){

        Button submit = (Button)findViewById(R.id.connect_btn);
        submit.setEnabled(false);

        runOnUiThread(this.retriever);
    }
}
Enter fullscreen mode Exit fullscreen mode

But the runOnUiThread does not allow me to perform networking. Therefore, do you have any Idea how I can run a thread that performs networking;

Top comments (0)