public class

ViewModelStore

extends java.lang.Object

 java.lang.Object

↳androidx.lifecycle.ViewModelStore

Gradle dependencies

compile group: 'androidx.lifecycle', name: 'lifecycle-viewmodel', version: '2.5.0-rc01'

  • groupId: androidx.lifecycle
  • artifactId: lifecycle-viewmodel
  • version: 2.5.0-rc01

Artifact androidx.lifecycle:lifecycle-viewmodel:2.5.0-rc01 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.lifecycle:lifecycle-viewmodel android.arch.lifecycle:viewmodel

Androidx class mapping:

androidx.lifecycle.ViewModelStore android.arch.lifecycle.ViewModelStore

Overview

Class to store ViewModels.

An instance of ViewModelStore must be retained through configuration changes: if an owner of this ViewModelStore is destroyed and recreated due to configuration changes, new instance of an owner should still have the same old instance of ViewModelStore.

If an owner of this ViewModelStore is destroyed and is not going to be recreated, then it should call ViewModelStore.clear() on this ViewModelStore, so ViewModels would be notified that they are no longer used.

Use ViewModelStoreOwner.getViewModelStore() to retrieve a ViewModelStore for activities and fragments.

Summary

Constructors
publicViewModelStore()

Methods
public final voidclear()

Clears internal storage and notifies ViewModels that they are no longer used.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public ViewModelStore()

Methods

public final void clear()

Clears internal storage and notifies ViewModels that they are no longer used.

Source

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.lifecycle;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * Class to store {@code ViewModels}.
 * <p>
 * An instance of {@code ViewModelStore} must be retained through configuration changes:
 * if an owner of this {@code ViewModelStore} is destroyed and recreated due to configuration
 * changes, new instance of an owner should still have the same old instance of
 * {@code ViewModelStore}.
 * <p>
 * If an owner of this {@code ViewModelStore} is destroyed and is not going to be recreated,
 * then it should call {@link #clear()} on this {@code ViewModelStore}, so {@code ViewModels} would
 * be notified that they are no longer used.
 * <p>
 * Use {@link ViewModelStoreOwner#getViewModelStore()} to retrieve a {@code ViewModelStore} for
 * activities and fragments.
 */
public class ViewModelStore {

    private final HashMap<String, ViewModel> mMap = new HashMap<>();

    final void put(String key, ViewModel viewModel) {
        ViewModel oldViewModel = mMap.put(key, viewModel);
        if (oldViewModel != null) {
            oldViewModel.onCleared();
        }
    }

    final ViewModel get(String key) {
        return mMap.get(key);
    }

    Set<String> keys() {
        return new HashSet<>(mMap.keySet());
    }

    /**
     *  Clears internal storage and notifies ViewModels that they are no longer used.
     */
    public final void clear() {
        for (ViewModel vm : mMap.values()) {
            vm.clear();
        }
        mMap.clear();
    }
}