Zohab Ali
Zohab Ali

Reputation: 9574

How can I save function calls in a list and call them one by one in Android

I am saving my function call like this

val savedFun = { myFunction("Ali", "[email protected]") }

I can call it latter like this savedFun() and its working perfectly fine. But I want to save multiple calls in a list and then call them one by one. How can I achieve that?

Upvotes: 0

Views: 506

Answers (1)

Aaron
Aaron

Reputation: 3894

You should be able to store your functions in a List<() -> Unit>:

val listOfFun: MutableList<() -> Unit> = mutableListOf()

listOfFun += { myFunction("Ali", "[email protected]") }
listOfFun += { myFunction("AnotherPerson", "[email protected]") }

And execute your functions from the list:

listOfFun.forEach { it() }

Upvotes: 2

Related Questions