69 lines
2.3 KiB
Java
69 lines
2.3 KiB
Java
/*
|
|
* Copyright (C) 2020 The Dagger Authors.
|
|
*
|
|
* 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 dagger.hilt.android.lifecycle;
|
|
|
|
import dagger.hilt.GeneratesRootInput;
|
|
import java.lang.annotation.ElementType;
|
|
import java.lang.annotation.Retention;
|
|
import java.lang.annotation.RetentionPolicy;
|
|
import java.lang.annotation.Target;
|
|
|
|
/**
|
|
* Identifies a {@link androidx.lifecycle.ViewModel} for construction injection.
|
|
*
|
|
* <p>The {@code ViewModel} annotated with {@link HiltViewModel} will be available for creation by
|
|
* the {@link dagger.hilt.android.lifecycle.HiltViewModelFactory} and can be retrieved by default in
|
|
* an {@code Activity} or {@code Fragment} annotated with {@link
|
|
* dagger.hilt.android.AndroidEntryPoint}. The {@code HiltViewModel} containing a constructor
|
|
* annotated with {@link javax.inject.Inject} will have its dependencies defined in the constructor
|
|
* parameters injected by Dagger's Hilt.
|
|
*
|
|
* <p>Example:
|
|
*
|
|
* <pre>
|
|
* @HiltViewModel
|
|
* public class DonutViewModel extends ViewModel {
|
|
* @Inject
|
|
* public DonutViewModel(SavedStateHandle handle, RecipeRepository repository) {
|
|
* // ...
|
|
* }
|
|
* }
|
|
* </pre>
|
|
*
|
|
* <pre>
|
|
* @AndroidEntryPoint
|
|
* public class CookingActivity extends AppCompatActivity {
|
|
* public void onCreate(Bundle savedInstanceState) {
|
|
* DonutViewModel vm = new ViewModelProvider(this).get(DonutViewModel.class);
|
|
* }
|
|
* }
|
|
* </pre>
|
|
*
|
|
* <p>Exactly one constructor in the {@code ViewModel} must be annotated with {@code Inject}.
|
|
*
|
|
* <p>Only dependencies available in the {@link dagger.hilt.android.components.ViewModelComponent}
|
|
* can be injected into the {@code ViewModel}.
|
|
*
|
|
* <p>
|
|
*
|
|
* @see dagger.hilt.android.components.ViewModelComponent
|
|
*/
|
|
@Target(ElementType.TYPE)
|
|
@Retention(RetentionPolicy.CLASS)
|
|
@GeneratesRootInput
|
|
public @interface HiltViewModel {}
|