1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.lifecycle; 18 19 import android.app.Application; 20 21 import androidx.annotation.MainThread; 22 import androidx.fragment.app.Fragment; 23 import androidx.fragment.app.FragmentActivity; 24 25 import org.jspecify.annotations.NonNull; 26 import org.jspecify.annotations.Nullable; 27 28 /** 29 * Utilities methods for {@link ViewModelStore} class. 30 * 31 * @deprecated Use the constructors for {@link ViewModelProvider} directly. 32 */ 33 @Deprecated 34 public class ViewModelProviders { 35 36 /** 37 * @deprecated This class should not be directly instantiated 38 */ 39 @Deprecated ViewModelProviders()40 public ViewModelProviders() { 41 } 42 43 /** 44 * Creates a {@link ViewModelProvider}, which retains ViewModels while a scope of given 45 * {@code fragment} is alive. More detailed explanation is in {@link ViewModel}. 46 * <p> 47 * It uses the {@link Fragment#getDefaultViewModelProviderFactory() default factory} 48 * to instantiate new ViewModels. 49 * 50 * @param fragment a fragment, in whose scope ViewModels should be retained 51 * @return a ViewModelProvider instance 52 * @deprecated Use the 'by viewModels()' Kotlin property delegate or 53 * {@link ViewModelProvider#ViewModelProvider(ViewModelStoreOwner)}, 54 * passing in the fragment. 55 */ 56 @Deprecated 57 @MainThread of(@onNull Fragment fragment)58 public static @NonNull ViewModelProvider of(@NonNull Fragment fragment) { 59 return new ViewModelProvider(fragment); 60 } 61 62 /** 63 * Creates a {@link ViewModelProvider}, which retains ViewModels while a scope of given Activity 64 * is alive. More detailed explanation is in {@link ViewModel}. 65 * <p> 66 * It uses the {@link FragmentActivity#getDefaultViewModelProviderFactory() default factory} 67 * to instantiate new ViewModels. 68 * 69 * @param activity an activity, in whose scope ViewModels should be retained 70 * @return a ViewModelProvider instance 71 * @deprecated Use the 'by viewModels()' Kotlin property delegate or 72 * {@link ViewModelProvider#ViewModelProvider(ViewModelStoreOwner)}, 73 * passing in the activity. 74 */ 75 @Deprecated 76 @MainThread of(@onNull FragmentActivity activity)77 public static @NonNull ViewModelProvider of(@NonNull FragmentActivity activity) { 78 return new ViewModelProvider(activity); 79 } 80 81 /** 82 * Creates a {@link ViewModelProvider}, which retains ViewModels while a scope of given 83 * {@code fragment} is alive. More detailed explanation is in {@link ViewModel}. 84 * <p> 85 * It uses the given {@link ViewModelProvider.Factory} to instantiate new ViewModels. 86 * 87 * @param fragment a fragment, in whose scope ViewModels should be retained 88 * @param factory a {@code ViewModelProvider.Factory} to instantiate new ViewModels 89 * @return a ViewModelProvider instance 90 * @deprecated Use the 'by viewModels()' Kotlin property delegate or 91 * {@link ViewModelProvider#ViewModelProvider(ViewModelStoreOwner, ViewModelProvider.Factory)}, 92 * passing in the fragment and factory. 93 */ 94 @Deprecated 95 @MainThread of(@onNull Fragment fragment, ViewModelProvider.@Nullable Factory factory)96 public static @NonNull ViewModelProvider of(@NonNull Fragment fragment, 97 ViewModelProvider.@Nullable Factory factory) { 98 if (factory == null) { 99 factory = fragment.getDefaultViewModelProviderFactory(); 100 } 101 return new ViewModelProvider(fragment.getViewModelStore(), factory); 102 } 103 104 /** 105 * Creates a {@link ViewModelProvider}, which retains ViewModels while a scope of given Activity 106 * is alive. More detailed explanation is in {@link ViewModel}. 107 * <p> 108 * It uses the given {@link ViewModelProvider.Factory} to instantiate new ViewModels. 109 * 110 * @param activity an activity, in whose scope ViewModels should be retained 111 * @param factory a {@code ViewModelProvider.Factory} to instantiate new ViewModels 112 * @return a ViewModelProvider instance 113 * @deprecated Use the 'by viewModels()' Kotlin property delegate or 114 * {@link ViewModelProvider#ViewModelProvider(ViewModelStoreOwner, ViewModelProvider.Factory)}, 115 * passing in the activity and factory. 116 */ 117 @Deprecated 118 @MainThread of(@onNull FragmentActivity activity, ViewModelProvider.@Nullable Factory factory)119 public static @NonNull ViewModelProvider of(@NonNull FragmentActivity activity, 120 ViewModelProvider.@Nullable Factory factory) { 121 if (factory == null) { 122 factory = activity.getDefaultViewModelProviderFactory(); 123 } 124 return new ViewModelProvider(activity.getViewModelStore(), factory); 125 } 126 127 /** 128 * {@link ViewModelProvider.Factory} which may create {@link AndroidViewModel} and 129 * {@link ViewModel}, which have an empty constructor. 130 * 131 * @deprecated Use {@link ViewModelProvider.AndroidViewModelFactory} 132 */ 133 @SuppressWarnings("WeakerAccess") 134 @Deprecated 135 public static class DefaultFactory extends ViewModelProvider.AndroidViewModelFactory { 136 /** 137 * Creates a {@code AndroidViewModelFactory} 138 * 139 * @param application an application to pass in {@link AndroidViewModel} 140 * @deprecated Use {@link ViewModelProvider.AndroidViewModelFactory} or 141 * {@link ViewModelProvider.AndroidViewModelFactory#getInstance(Application)}. 142 */ 143 @Deprecated DefaultFactory(@onNull Application application)144 public DefaultFactory(@NonNull Application application) { 145 super(application); 146 } 147 } 148 } 149