how to call interface method in view model of fragment class in kotlin










1















I have an Activity which is called Homeactivity and I have a fragment Notification and their ViewModel class, I want to update notification count from fragment's ViewModel and I declared that interface outside the notification ViewModel and I'm unable to call the method of that interface in Kotlin



class NotificationsViewModel(val notificationsActivity: HomeActivity) : 
BaseObservable()


//somewhere i want to update count which is in homeactivity


interface NotifyCount
fun notifyNotificationCount(count: String)




My fragment class is given below



class NotificationsFragment : Fragment() 
private var notificationsBinding: NotificationsActivityBinding? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
notificationsBinding= DataBindingUtil.inflate(inflater, R.layout.notifications_activity,container,false)
notificationsBinding!!.viewModel= NotificationsViewModel(activity!! as HomeActivity)
return notificationsBinding!!.root





I just want to call method notifyNotificationCount in ViewModel so that I will update my count from HomeActivity by overriding the NotifyCount



how can I call the method notifyNotificationCount in
viewModel` class?










share|improve this question
























  • Have your read this? developer.android.com/topic/libraries/architecture/…

    – Andre Artus
    Nov 12 '18 at 11:20











  • Also, this: medium.com/mindorks/…

    – Andre Artus
    Nov 12 '18 at 11:22











  • @AndreArtus i will try.

    – farhana
    Nov 12 '18 at 11:58















1















I have an Activity which is called Homeactivity and I have a fragment Notification and their ViewModel class, I want to update notification count from fragment's ViewModel and I declared that interface outside the notification ViewModel and I'm unable to call the method of that interface in Kotlin



class NotificationsViewModel(val notificationsActivity: HomeActivity) : 
BaseObservable()


//somewhere i want to update count which is in homeactivity


interface NotifyCount
fun notifyNotificationCount(count: String)




My fragment class is given below



class NotificationsFragment : Fragment() 
private var notificationsBinding: NotificationsActivityBinding? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
notificationsBinding= DataBindingUtil.inflate(inflater, R.layout.notifications_activity,container,false)
notificationsBinding!!.viewModel= NotificationsViewModel(activity!! as HomeActivity)
return notificationsBinding!!.root





I just want to call method notifyNotificationCount in ViewModel so that I will update my count from HomeActivity by overriding the NotifyCount



how can I call the method notifyNotificationCount in
viewModel` class?










share|improve this question
























  • Have your read this? developer.android.com/topic/libraries/architecture/…

    – Andre Artus
    Nov 12 '18 at 11:20











  • Also, this: medium.com/mindorks/…

    – Andre Artus
    Nov 12 '18 at 11:22











  • @AndreArtus i will try.

    – farhana
    Nov 12 '18 at 11:58













1












1








1








I have an Activity which is called Homeactivity and I have a fragment Notification and their ViewModel class, I want to update notification count from fragment's ViewModel and I declared that interface outside the notification ViewModel and I'm unable to call the method of that interface in Kotlin



class NotificationsViewModel(val notificationsActivity: HomeActivity) : 
BaseObservable()


//somewhere i want to update count which is in homeactivity


interface NotifyCount
fun notifyNotificationCount(count: String)




My fragment class is given below



class NotificationsFragment : Fragment() 
private var notificationsBinding: NotificationsActivityBinding? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
notificationsBinding= DataBindingUtil.inflate(inflater, R.layout.notifications_activity,container,false)
notificationsBinding!!.viewModel= NotificationsViewModel(activity!! as HomeActivity)
return notificationsBinding!!.root





I just want to call method notifyNotificationCount in ViewModel so that I will update my count from HomeActivity by overriding the NotifyCount



how can I call the method notifyNotificationCount in
viewModel` class?










share|improve this question
















I have an Activity which is called Homeactivity and I have a fragment Notification and their ViewModel class, I want to update notification count from fragment's ViewModel and I declared that interface outside the notification ViewModel and I'm unable to call the method of that interface in Kotlin



class NotificationsViewModel(val notificationsActivity: HomeActivity) : 
BaseObservable()


//somewhere i want to update count which is in homeactivity


interface NotifyCount
fun notifyNotificationCount(count: String)




My fragment class is given below



class NotificationsFragment : Fragment() 
private var notificationsBinding: NotificationsActivityBinding? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
notificationsBinding= DataBindingUtil.inflate(inflater, R.layout.notifications_activity,container,false)
notificationsBinding!!.viewModel= NotificationsViewModel(activity!! as HomeActivity)
return notificationsBinding!!.root





I just want to call method notifyNotificationCount in ViewModel so that I will update my count from HomeActivity by overriding the NotifyCount



how can I call the method notifyNotificationCount in
viewModel` class?







android kotlin interface viewmodel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 10:50







farhana

















asked Nov 12 '18 at 10:32









farhanafarhana

2,28941932




2,28941932












  • Have your read this? developer.android.com/topic/libraries/architecture/…

    – Andre Artus
    Nov 12 '18 at 11:20











  • Also, this: medium.com/mindorks/…

    – Andre Artus
    Nov 12 '18 at 11:22











  • @AndreArtus i will try.

    – farhana
    Nov 12 '18 at 11:58

















  • Have your read this? developer.android.com/topic/libraries/architecture/…

    – Andre Artus
    Nov 12 '18 at 11:20











  • Also, this: medium.com/mindorks/…

    – Andre Artus
    Nov 12 '18 at 11:22











  • @AndreArtus i will try.

    – farhana
    Nov 12 '18 at 11:58
















Have your read this? developer.android.com/topic/libraries/architecture/…

– Andre Artus
Nov 12 '18 at 11:20





Have your read this? developer.android.com/topic/libraries/architecture/…

– Andre Artus
Nov 12 '18 at 11:20













Also, this: medium.com/mindorks/…

– Andre Artus
Nov 12 '18 at 11:22





Also, this: medium.com/mindorks/…

– Andre Artus
Nov 12 '18 at 11:22













@AndreArtus i will try.

– farhana
Nov 12 '18 at 11:58





@AndreArtus i will try.

– farhana
Nov 12 '18 at 11:58












1 Answer
1






active

oldest

votes


















0














You can use MutableLiveData in your ViewModel class and observe the value of notification count, whenever the value of notification count changed it will inform to observing the class.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260283%2fhow-to-call-interface-method-in-view-model-of-fragment-class-in-kotlin%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can use MutableLiveData in your ViewModel class and observe the value of notification count, whenever the value of notification count changed it will inform to observing the class.






    share|improve this answer



























      0














      You can use MutableLiveData in your ViewModel class and observe the value of notification count, whenever the value of notification count changed it will inform to observing the class.






      share|improve this answer

























        0












        0








        0







        You can use MutableLiveData in your ViewModel class and observe the value of notification count, whenever the value of notification count changed it will inform to observing the class.






        share|improve this answer













        You can use MutableLiveData in your ViewModel class and observe the value of notification count, whenever the value of notification count changed it will inform to observing the class.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 11:23









        Virendra VarmaVirendra Varma

        14329




        14329



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260283%2fhow-to-call-interface-method-in-view-model-of-fragment-class-in-kotlin%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus