Bhavin Patel
Bhavin Patel

Reputation: 912

Dagger Hilt @ViewModelInject is deprecated how to use @ApplicationContext in @HiltViewModel

It was android studio problem I think. It's started working automatically after 2 days maybe Restart android studio that's all it takes. I was using 2.31.2-alpha version.

I'm using @ViewModelInject in my ViewModel as shown in below but now It's deprecated so When I tried to use @HiltViewModel but I can't use @ApplicationContext init.

So my question is How to use common dependency which I annotated with @InstallIn(SingletonComponent::class) in @HiltViewModel ?

How to use @ApplicationContext in @HiltViewModel , ViewModelComponent::class ?

My code Which Work fine with @ViewModelInject are below

1. AppModule()

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class SplashApiInterface

@Module
@InstallIn(SingletonComponent::class)
class AppModule() {
    internal var pref_name = Common.pref_name

    @Provides
    @Singleton
    fun mySharedPreference(@ApplicationContext context: Context): SharedPreferences {
        return context.getSharedPreferences(pref_name, Context.MODE_PRIVATE)
    }

    @Provides
    @Singleton
    fun connectionDetector(@ApplicationContext context: Context): ConnectionDetector {
        return ConnectionDetector(context)
    }

    @Provides
    fun myCompositeDisposable(): CompositeDisposable {
        return CompositeDisposable()
    }

    @SplashApiInterface
    @Provides
    @Singleton
    fun mAPIServiceSplash(@ApplicationContext context: Context): ApiInterface {
        return ApiUtils.getAPIServiceSpl(context)
    }

}`

2.SplashVModel

@ActivityScoped
@Singleton
class SplashVModel @ViewModelInject constructor(@ApplicationContext val context: Context,
                                                val sharedPreferences: SharedPreferences,
                                                @SplashApiInterface val mAPIService: ApiInterface,
                                                val myCompositeDisposable: CompositeDisposable,
                                                var cd: ConnectionDetector
) : ViewModel() {
    
    // here I removed use cases of constructor value for brevity

    }

}

So now if I use @HiltViewModel how to use SingletonComponent common function? Now If create ViewModelComponent::class then how to use that common function again in this? So What should I do ? Do I have to remove all common cases from SingletonComponent and use individually in each ViewModel()?

Upvotes: 2

Views: 2163

Answers (3)

CryptoCode
CryptoCode

Reputation: 1117

It is a good practice (but not mandatory) to keep ViewModel free from Android framework references like Activity, Context, Drawables etc. So passing in a context to ViewModel is not recommend. AndroidViewModel comes with same issue — it’ll hold a reference to Android framework. Stop passing Context into ViewModels

Upvotes: 0

Anubhav
Anubhav

Reputation: 2190

@ViewModelInject has been deprecated and has been replaced by @HiltViewModel.

The ViewModel annotated with HiltViewModel will be available for creation by HiltViewModelFactory. The HiltViewModel containing a constructor annotated with Inject will have its dependencies defined in the constructor parameters injected by Dagger's Hilt. https://dagger.dev/api/latest/dagger/hilt/android/lifecycle/HiltViewModel

A simple ViewModel will now look like :

@HiltViewModel
class MainViewModel @Inject constructor() :
ViewModel() {

}

Now Hilt provides some predefined qualifiers. For example, as you might need the Context class from either the application or the activity, Hilt provides the @ApplicationContext and @ActivityContext qualifiers.

@HiltViewModel
class MainViewModel @Inject constructor(@ApplicationContext 
private val context: ApplicationContext) :
ViewModel() {

}

Upvotes: 1

Andrew
Andrew

Reputation: 4742

A Viewmodel should not be annotated with @ActivityScoped or @Singleton, as dagger-hilt will provide this instance in a rather "special" way and not as usual as it provides the other dependencies (lifecycle and stuff).

First make sure, that you have the following dependencies and there are all up to date:

    implementation "com.google.dagger:hilt-android:2.32-alpha"
    kapt "com.google.dagger:hilt-compiler:2.32-alpha"
    kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha03'

    implementation "androidx.hilt:hilt-lifecycle-viewmodel:2.32-alpha"

As you didn't provide any details about what's not "working", this should:

@HiltViewModel
class YourViewModel @Inject constructor(
    @Applicationcontext context: Context
) : ViewModel()

Upvotes: 0

Related Questions