Hi, I have a doubt, How do we call an interface me...
# android
s
Hi, I have a doubt, How do we call an interface method in fragment from parent activity in kotlin. When I try to do that it says the interface object must be initialised
g
Please share code example
s
I created an interface in mainactivity
Copy code
interface onProductsChangedListner{
        fun updateProductList(products:List<Product>)
}
and implemented the same in a fragment
Copy code
class GalleryFragment: Fragment(),MainActivity.onProductsChangedListner {
    override fun updateProductList(products: List<Product>) {
        this.products = products
        galleryAdapter.changeDataset(products)
    }
How do i call the interface methods from main activity?
When i try to create an object of interface class, kotlin says it must be initialised. It is not required in java right?. So what should i do?
g
How do you call method from activity?
s
I tried doing this
Copy code
var productChangedListner: onProductsChangedListner
productChangedListner.updateProductList(products)
but it says productChangedListner must be initialised
g
But looks like you try to access the same property that initializing (also, where is equals between type and assignment)
Where is fragment in this assignment?
s
Im implementing the interface in fragment
g
And? Maybe could you just post screenshot from your IDE with error message and your code. Because your code snippet above is just syntactically wrong
s
I figured it out. I guess it was syntactically wrong. Thank you for pointingthat out