# Android : ViewModelStore

Ever wondered how `ViewModel` survive during configuration changes in our Android apps? 🤔

Let me introduce you to `ViewModelStore`!

### 🔍 **What is**`ViewModelStore`?

`ViewModelStore` is a class that stores and manages `ViewModel` instances. It's essential for keeping our `ViewModel`s alive during configuration changes like screen rotations.

### 📌 **Key Features:**

* **Retain**`ViewModel`s: Keeps `ViewModel`s during configuration changes.
    
* **Efficient Resource Management:** Automatically clears and notifies `ViewModel`s when they are no longer needed, preventing memory leaks.
    
* **Easy Access:** Retrieve `ViewModelStore` instances using `ViewModelStoreOwner.getViewModelStore`.
    

### 🛠️ **How Does it Work?**

1. **Storing ViewModels:** Use the `put` method to add a `ViewModel` with a unique key. If a `ViewModel` with the same key exists, it's replaced and the old one is cleared.
    
2. **Retrieving ViewModels:** Use the `get` method to fetch a `ViewModel` by its key.
    
3. **Listing Keys:** Use the `keys` method to get a set of all keys.
    
4. **Clearing ViewModels:** Call the `clear` method to notify and remove all `ViewModel`s.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719476538732/6d007f27-4fd0-4866-bb01-9487d7d931f5.png align="center")

Implementing `ViewModel` in our app can significantly enhance its stability and performance. Give it a try and see the difference!

---

That's it for today. Happy Coding...

%%[buymeacoffee-donate]
