2

I am using Delegation in Kotlin. So therefore I have base class which handles common network result but only difference is Data(Model class)

class BaseDataDelegation<T>(var oldData: T): WebDataListener<T> { override fun onSuccess(data: T) { oldData = data //oldData's original variable value which is inside main activity should also be updated /.../ } override fun onFailed() { /.../ } } 

then In MainActivity I'm calling

dataManager.getResponse(BaseDataDelegation(oldData))//in DataManager.getResponse(listener:WebDataListener<T>) 

Now as I passed oldData to BaseDataDelegation, so when value of oldData is changed in BaseDataDelegation class it should reflect back to variable of MainActivty.
How can I do this in Kotlin?

1 Answer 1

4

You can use a mutable property reference to achieve this. Here's an example:

class BaseDelegation<T>(val property: KMutableProperty0<T>) { override fun onSuccess(data: T) { property.set(data) } } 

Then, to construct a BaseDelegation, use a bound reference to a property, e.g. this::oldData or myActivity::oldData (the property itself should be mutable, i.e. var).

Here's a simplified runnable demo: (link)

Sign up to request clarification or add additional context in comments.

2 Comments

looks like it will work , i will update you after implementing it .
first of all thanks it is working but I don't understand much about it that how it is working,i have read documentation but not very helpful , can you please explain more

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.