Popular

Room + ViewModel + LiveData + RecyclerView (MVVM) Part 5 - VIEWMODEL - Android Studio Tutorial

Published on Sep 20, 2018 31,017 views

In part 5 of the Architecture Components tutorial, we will create our Viewmodel class.
The ViewModel works as a gateway between the UI controller and the repository. It stores and processes data for the activity/fragment and it doesn't get destoyed on configuration changes, so it doesn't lose it's variable state for example when the device is rotated.
By extending AndroidViewModel, we get a handle to the application context, which we then use to instantiate our RoomDatabase.
In our activity we let the system provide us the correct ViewModel instance by calling ViewModelProviders.of, where we pass the Activity or Fragment this ViewModel's lifecycle should be scoped to. When our Activity/Fragment is then destroyed, the ViewModel will go through it's onCleared method and get removed from the memory.
In our activitie's onCreate method, we retrieve the LiveData that is stored in our ViewModel and call observe on it, where we pass a LifecycleOwner and an Observer. In the onChange callback we get an update on our data whenever something in the corresponding database table changes. The LiveData will automatically start and stop sending updates to our activity at the right time in it's lifecycle and clean up any unused references.


Watch the whole playlist:
https://www.youtube.com/playlist?list...

Example code, dependencies & instructions:
https://codinginflow.com/tutorials/an...

____________________
Subscribe to my channel:
https://www.youtube.com/c/codinginflo...

Want more Android tutorials? Check my playlist section:
https://www.youtube.com/c/codinginflo...


Follow me on social media:

Facebook:
https://www.facebook.com/codinginflow

Instagram:
https://www.instagram.com/codinginflow

Twitter:
https://twitter.com/codinginflow

Blog:
https://codinginflow.com/

Comments
  • Üsame Savas
    Üsame Savas 4 месяца назад You explain much better than the documentation, every single detail, logic and purpose behind them. No missing points. A real tutorial! Thanks for this amazing work!
  • Coding in Flow
    Coding in Flow 4 месяца назад Thank you for the kind words! 💚😀👍
  • Antonio Gonzalez
    Antonio Gonzalez 6 месяцев назад Dude, you're good with really good quality videos and examples. I hope your channel picks up the ground! A suggestion/personal request, I've been trying to get into Kotlin, Dagger2 and Rx, but there are no good step by step tutorials on it (and I've searched for them). I know the basics of them all but until I actually do practice, it's hard for the concepts to stick. If you wonder what to do next, that's a good area to go to. (I think!)
  • Coding in Flow
    Coding in Flow 6 месяцев назад Thank you! I am currently learning Kotlin and Dagger 2 and hope I can make tutorials on those 2 soon!
  • Ibrahim Elmasry
    Ibrahim Elmasry 8 месяцев назад awesome tutorial you're the best ^_^ keep going (Y)
  • Coding in Flow
    Coding in Flow 8 месяцев назад Thank your for the kind words!
  • Jaykrushna Solanki
    Jaykrushna Solanki 7 месяцев назад Thank You Sir. Finely Found Good Explain in Simple Way on Android Architecture Components.
  • Coding in Flow
    Coding in Flow 7 месяцев назад Thank you! I am happy it helped!
  • Vic Dance
    Vic Dance 4 месяца назад Thank you so much. I have to do a school project and you are helping me so much. I hope life treat you good.
  • Coding in Flow
    Coding in Flow 4 месяца назад Thank you very much! I am happy that it helps!
  • hashir saeed
    hashir saeed 6 месяцев назад Activitiyaa :D. Man you're good. Keep up the Good work. :) Thanks Alot.
  • Coding in Flow
    Coding in Flow 6 месяцев назад Thank you my friend!
  • Vengatesh Murugasamy
    Vengatesh Murugasamy 7 месяцев назад Crisp and upto the point.Thanks for the video.
  • Coding in Flow
    Coding in Flow 7 месяцев назад You're welcome! Thanks for watching!
  • Jennifer Alderson
    Jennifer Alderson 3 месяца назад The best android tutorials I have ever seen. Thank you very much!
  • Coding in Flow
    Coding in Flow 3 месяца назад Thank you for the kind words!
  • Floyd Asio
    Floyd Asio 5 месяцев назад Sir ViewModelProviders was deprecate.. what is the replacement and how to use it? newbi here.. Thanks
  • Mark Abrasaldo
    Mark Abrasaldo 4 месяца назад You found the solution bro? I am also facing the same problem.
  • Coding in Flow
    Coding in Flow 4 месяца назад I think it has something to do with the dependencies you added. Do you use the latest version of Android Studio?
  • Flutter Studio
    Flutter Studio 4 месяца назад @Mark Abrasaldo https://stackoverflow.com/questions/53903762/viewmodelproviders-is-deprecated-in-1-1-0
  • Spectral Fergus
    Spectral Fergus 4 месяца назад ViewModelProviders is not deprecated. Don't worry, it confused me too. The Android docs do say that the constructor is deprecated, since you shouldn't instantiate one yourself. Instead, the class opens up its "public static" utility methods to you from the ViewModelStore. In "ViewModelProviders.of()", "of()" is one of those utility methods. We do the same thing when creating the RoomDatabase singleton: the constructor is private, so our MainActivity can't instantiate one directly. Instead, we opened up a "public static getInstance()" method that handled making one the correct way. The StackOverflow link leads to the same conclusion, and it sounds like the user who posted that problem just put their Lifecycle dependency in the wrong place. (I know you've probably moved on since 1 month ago, but I hope I can help any other new devs reading this)
  • Coding in Flow
    Coding in Flow 4 месяца назад Spectral Fergus Thanks for the explanation, I am sure it will help lots of people!
  • Pro People Pty Ltd
    Pro People Pty Ltd 2 месяца назад These videos are fantastic, but only hanging on by my fingernails on this series, any chance of code update (ie more specific details to Spectral's reply) on this ViewModelProviders problem. thanks
  • Spectral Fergus
    Spectral Fergus 2 месяца назад @Pro People Pty Ltd the code provided actually works exactly as it should. Any problems are likely due to your dependencies in the gradle file (wrong place, wrong version #, or using appcompat dependencies after migrating to androidx etc.). I found this series to be the most helpful I've ever seen on this topic and strongly recommend watching it again as you're fiddling around with your app. It'll start to make sense!
  • Pro People Pty Ltd
    Pro People Pty Ltd 2 месяца назад Spectral, thanks for quick response, you are correct, I have temporarily reverted back to android.arch.lifecycle.LiveData; from androidx.lifecycle.LiveData; and it works as it should, (will make the leap back to "X" at a later time), and agree not only this series (been through it several times), but all of Coding in flow videos are amazing, so complete and clear, good speed to follow without time wasting, brilliant.
  • Jovica Margit
    Jovica Margit 2 месяца назад @Spectral Fergus Sorry, but I'm not sure what I need to do to fix this problem?
  • Pramod Ranasinghe
    Pramod Ranasinghe 1 месяц назад @Flutter Studio It worked bro!!!
  • Piet Baudoin
    Piet Baudoin 1 месяц назад I can't seem to fix it. I have the dependencies for Android X imported but it still can't resolve the 'of' method.
  • Pallavi Singh
    Pallavi Singh 1 месяц назад This is a fantastic hands on. All important concepts covered. Thanks!
  • Coding in Flow
    Coding in Flow 1 месяц назад Thanks for the compliment :)
  • IOS Development
    IOS Development 8 месяцев назад You sir are a great teacher - Thank you.
  • Coding in Flow
    Coding in Flow 8 месяцев назад Thank your for the kind words!
  • Mkhitar Hakobyan
    Mkhitar Hakobyan Неделю назад Thanks, you are the best !!!
  • Coding in Flow
    Coding in Flow Неделю назад Thank you very much! You're very welcome!
  • Shaikh Aaves
    Shaikh Aaves 8 месяцев назад Thanks! and When will be the next one?
  • Coding in Flow
    Coding in Flow 8 месяцев назад It will be out in a couple days
  • Emmanuel Okonji
    Emmanuel Okonji 7 месяцев назад expecting it, thank you
  • Gaga Antic
    Gaga Antic 1 месяц назад I'm really enjoying this series!!! Thank you!!!!!
  • Coding in Flow
    Coding in Flow 1 месяц назад I am very happy to hear that!
  • tej
    tej 8 месяцев назад Awesome 👍🏻 please make a video on dependency injection, thank you 😊
  • Coding in Flow
    Coding in Flow 8 месяцев назад Yes I will do a series on Dagger 2 soon
  • Shaikh Aaves
    Shaikh Aaves 8 месяцев назад Can you please make the recyclerView (adapter) using the Paging library! Please!
  • Coding in Flow
    Coding in Flow 8 месяцев назад I will do that later in a separate video series
  • java lover
    java lover 8 месяцев назад thank you so much :)
  • Coding in Flow
    Coding in Flow 8 месяцев назад You're welcome!
  • Stefan C
    Stefan C 8 месяцев назад This is great! Looking forward to the next video !
  • Coding in Flow
    Coding in Flow 8 месяцев назад Thank you! It will be out soon!
  • Ehsan Mrf
    Ehsan Mrf 5 месяцев назад Excellent _ Thanks so much
  • Coding in Flow
    Coding in Flow 5 месяцев назад And again!
  • Corneille Eddy
    Corneille Eddy 8 месяцев назад Great. Thank you sir
  • Coding in Flow
    Coding in Flow 8 месяцев назад You're welcome brother!
  • Corneille Eddy
    Corneille Eddy 8 месяцев назад Waiting for mvvm retrofit and dagger 2 tutorial Sir 🙏
  • Coding in Flow
    Coding in Flow 8 месяцев назад Dagger 2 will follow later