Android MVVM Kotlin Tutorial – LiveData + ViewModel (Android Architecture Components)

📗 Read the written tutorial with all the code 👇👇 Start saving time now by planning weekly with Week Sweep:

📗 Read the written tutorial with all the code 👇👇

Start saving time now by planning weekly with Week Sweep:

Model – View – ViewModel is an architectural pattern which will empower you to write manageable, maintainable, cleaner and testable code. MVVM is also supported and encouraged by Google itself. There are many first-party libraries like lifecycle-aware components, LiveData, ViewModel and many more.

In this tutorial you are going to put MVVM pattern into practice. You will build a simple, yet real-enough app which will make you understand MVVM on a deeper level. It will be an app displaying quotes which you put in. This quote app will have a ViewModel, Repository, fake database and a simple dependency injection. This will give you a strong foundation to build on.

Go to my website for more information, code examples and articles:

Follow me on social media:


28 Replies to “Android MVVM Kotlin Tutorial – LiveData + ViewModel (Android Architecture Components)”

  1. Memory LEAK.
    As for me it's not a good idea to receive LiveData from repository directly (through ViewModel) to Layout / Activvity / Fragment. Because you don't know how much time it will take to the DataBase to execute your query. It should be asynchronous task. That's why you should wait in ViewModel the answer from the Repository DB and then using switchMap replace the answer (LiveData ) from Repository to LiveData that is intended for your Layout.

    Summary: Get data from DB to ViewModel and if Activity / Fragment AND put the answer to the new LiveData that will be received in Activity / Frament.
    Why? Because user can run some task that needs DB data (REST API for example), and without waiting for the end of execution exit from Application or switched to another Activity / Fragment.

  2. Incredible tutorial!! Thank you so much for making this, you are an amazing teacher! I am currently making an app that handles the shopping in a retail store (for the people who physically shop or "pick" the items that are delivered to your house). I made a basic desktop version using java and MVC, but after seeing how well you put this together I am definitely going to use Kotlin and the MVVM techniques you have shown here. Much appreciated! Subscribed 😀

  3. Hi Reso Coder. I was following your example. I check there are some depreceated library and some code is not valid anymore. Do you want I send you the proyect updated so you can upload to GitHub?.

  4. 23:10 if your QuoteViewModel is only passing along the repos's method, it is an useless intermediate. In this case, you can work on the repo directly instead of adding complexity for the sake of it.

  5. I honestly don't think MVVM requires this many code and classes to be made. Like MVP, all you really need is a model class, view class, and a presenter class. DI can be done manually or with Dagger. That's it. With this, the codebase would clutter up as it scales making everything hard to maintain and unreadable.

  6. Really awesome video! It would be nice to provide an advanced implementation like this, combining mvvm with services, where data are driven from services to a database

  7. Thank you, you are really simple explained the difference module: dao, repos, vm, vmf, and use that in Activity ))
    That's not simple to understand that when you are reading the manual by himself. Three time "addQuote" 🙈👋

Beri komentar...