Kulwinder Singh
07/16/2019, 7:06 AMobject
in almost every activity, so therfore i have to create variables in each activity, i'm creating this variables to just use shorter names nothing else.
private val api
get() = UserRestApi.instance
private val authorization
get() = AccountUtils.userAuthToken
So now my question is that i'm thinking to create interface
like below.
interface Networking {
val api
get() = UserRestApi.instance
val authorization
get() = AccountUtils.userAuthToken
}
and then in each activity i will implement this interface and then i will directly access this api
and authorization
variables. so i don't have to create this variables in each activity, so like below i just have to implement
Networking
interface and i have access to this variables
class MyActivity : AppCompatActivity(), Networking {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.my_activity)
api.loadSomething(authorization)// so by implementing Networking i can access this fields
}
}
**1. So is something bad in this approach ?
2. is there any other solution to this problem in kotlin ?**jc
07/16/2019, 9:14 AMNetworking
object inside the MyActivity
. Very helpfull can by dependency injection for it.Vlad
07/16/2019, 9:16 AMobject Network {
val api by lazy { UserRestApi.instance }
}
and use it from any activity:
Network.api.loadSomething(...)
Eric Martori
07/16/2019, 9:19 AMVlad
07/16/2019, 9:39 AMViewModel
to launch
network calls in the viewModelScope
and use LiveData
to observe
the resultKulwinder Singh
07/16/2019, 9:48 AMlifecycleScope
to call network request also i'm using Retrofit with suspend
functions, i guess this might be enough to handle network request in Activity ?Vlad
07/16/2019, 10:01 AMSteve
07/16/2019, 5:31 PMEric
07/19/2019, 11:48 PM