Issue
I have the following function that can take an activity as an argument and when I call it from an Activity it works perfectly. Now that I want to call this function from a fragment but I can see there is an error in the editor saying 'Incompatible types: CargoFragment and Activity'. I tried replacing activity: Activity with context: Context.
The error I have is at 'is CargoFragment'
fun getProductList(activity: Activity) {
mFireStore.collection("abc")
.get()
.addOnSuccessListener {
.....
.....
.....
productList.add(product)
}
when (activity) {
is CargoActivity -> {
activity.success(productList)
}
is CheckoutActivity -> {
activity.success(productList)
}
is CargoFragment -> {
activity.success(productList)
}
}
}
.addOnFailureListener { e ->
Log.d("CheckTag", e.message!!)
when (activity) {
is CargoActivity -> {
activity.hideProgressDialog()
}
is CheckoutActivity -> {
activity.hideProgressDialog()
}
}
}
}
Solution
Don't pass activity to getProductList. As far as I understand, you are passing activity to execute some code when you get a response (success or failure). A better way to implement this is to expose callback lambdas.
Consider this approach:
fun getProductList(onSuccess: (List<Product>) -> Unit, onFailure:() -> Unit) {
mFireStore.collection("abc")
.get()
.addOnSuccessListener {
...
productList.add(product)
}
onSuccess(productList)
}
.addOnFailureListener { e ->
...
onFailure()
}
}
Usage (in your activity and fragment):
getProductList(
onSuccess = { list ->
success(list) // whatever you want to do on success
},
onFailure = {
hideProgressBar() // whatever you want to do on failure
}
)
Answered By - Arpit Shukla
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.