1b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets/* 2b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * Copyright (C) 2017 The Android Open Source Project 3b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * 4b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * Licensed under the Apache License, Version 2.0 (the "License"); 5b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * you may not use this file except in compliance with the License. 6b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * You may obtain a copy of the License at 7b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * 8b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * http://www.apache.org/licenses/LICENSE-2.0 9b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * 10b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * Unless required by applicable law or agreed to in writing, software 11b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * distributed under the License is distributed on an "AS IS" BASIS, 12b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * See the License for the specific language governing permissions and 14b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * limitations under the License. 15b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets */ 16b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets 17ba069d50913c3fb250bb60ec310439db36895337Alan Viverettepackage androidx.lifecycle; 18b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets 19b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinetsimport java.util.HashMap; 20b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets 21b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets/** 22b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * Class to store {@code ViewModels}. 23b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * <p> 24b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * An instance of {@code ViewModelStore} must be retained through configuration changes: 25b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * if an owner of this {@code ViewModelStore} is destroyed and recreated due to configuration 26b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * changes, new instance of an owner should still have the same old instance of 27b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * {@code ViewModelStore}. 28b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * <p> 29b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * If an owner of this {@code ViewModelStore} is destroyed and is not going to be recreated, 30b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * then it should call {@link #clear()} on this {@code ViewModelStore}, so {@code ViewModels} would 31b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * be notified that they are no longer used. 32b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * <p> 335122b0151b549a81b0c8cac9192b6713df0d81abIan Lake * Use {@link ViewModelStoreOwner#getViewModelStore()} to retrieve a {@code ViewModelStore} for 34b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * activities and fragments. 35b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets */ 36b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinetspublic class ViewModelStore { 37b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets 38b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets private final HashMap<String, ViewModel> mMap = new HashMap<>(); 39b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets 40b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets final void put(String key, ViewModel viewModel) { 416e19edb4d834ed13b95cceb270812af5f9b26a96Jake Wharton ViewModel oldViewModel = mMap.put(key, viewModel); 42b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets if (oldViewModel != null) { 43b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets oldViewModel.onCleared(); 44b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets } 45b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets } 46b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets 47b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets final ViewModel get(String key) { 48b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets return mMap.get(key); 49b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets } 50b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets 51b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets /** 52b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets * Clears internal storage and notifies ViewModels that they are no longer used. 53b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets */ 54b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets public final void clear() { 55b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets for (ViewModel vm : mMap.values()) { 56b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets vm.onCleared(); 57b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets } 58b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets mMap.clear(); 59b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets } 60b9920233c8c63b218ab8ca1f7df35fdc9649f12cSergey Vasilinets} 61