0

Let's take two generic methods:

  1. fooA(), which onSuccess, it updates activity A
  2. fooB(), which onSuccess invokes fooA()

that send an asynchronous Volley request each to my server.

I would like to avoid in Activity A this pattern:

class A : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_A)
        
        fooB()
    }

    fun fooB(): {
        volleyRequest() {
               onSuccess(): fooA()
               onFailure(): Log.e("ERROR")
        }
    }

    fun fooA(): {
         volleyRequest() {
               onSuccess(): // do something on UI
               onFailure(): Log.e("ERROR")
        }
    }
}

Instead, I would like to keep my code clean and define these requests in a separate RequestManager and have something like:

class A : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_A)

            RequestManager.fooB().onSuccess().RequestManager.fooA().onSuccess() {
                // do something on UI
            }
        }
    }

Separating methods in this way would allow me to reuse fooA() and fooB() in other ways throughout my application. Is it possible in Android (Kotlin) to deal with asynchronous tasks in this way?

0 Answers0