android – hilt – How can I update values after initialization


In my app, the user can log in from any fragment and so after login if I send any request to the server, the new token didn’t send.
for example, if the user login from fragment A and send a request the new token for the user didn’t send with the headers.
because hilt viewModel is created and the token value == null

how can I refresh any value I injected before?

object NetworkModule {

fun provideMainRepository(): MainRepository {
    return MainRepository()

fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
    return Retrofit.Builder()

fun provideOkHttpClient(token:String): OkHttpClient {
    val okHttpClient = OkHttpClient.Builder()

fun provideToken(@ApplicationContext context: Context): String {
    return context.getToken()

fun provideDataService(retrofit: Retrofit): DataService {
    return retrofit.create(


my ViewModel

class ProductsCategoryViewModel @Inject constructor(
private val service: CartService, private val mainRepository: MainRepository
) : BaseViewModel() {}

so how can I update the token after injected values created

Source: Android Questions

Categorized as android, dagger-2, dagger-hilt, dependency-injection, kotlin


Leave a Reply

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us