-1

I have a problem with injection dagger into view model.The problem is that losing form inputs when rotating the screen.Is the problem in my injection or initialize my view model? Here is my viewmodel;

@HiltViewModel
class ProfilIslemViewModel @Inject constructor(application: Application,
                                           private val kullaniciService: KullaniciService,
                                           private val kullaniciDao:KullaniciDao
): AndroidViewModel(application), CoroutineScope {...}

And here is my fragment that use this view model;

@AndroidEntryPoint
class ProfilIslemFragment:Fragment(), ProfilIslemFragmentClickListener {

private val viewModel: ProfilIslemViewModel by viewModels();
...
}

Here is the module that injected class;

@Module
@InstallIn(SingletonComponent::class)
object KutuphanemAppModule {

@Singleton
@Provides
fun provideKutuphanemDatabase
            (@ApplicationContext context:Context) = Room.databaseBuilder(
    context,
    KutuphanemDatabase::class.java,
    KUTUPHANEM_DB_NAME
).build();

@Singleton
@Provides
fun provideRetrofit(client: OkHttpClient):Retrofit =
    Retrofit.Builder()
        .baseUrl(API_URL)
        .addConverterFactory(ScalarsConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build();

@Singleton
@Provides
fun provideHttpClient(customSharedPreferences: CustomSharedPreferences):OkHttpClient{
    return OkHttpClient.Builder().addInterceptor(object:Interceptor{
        override fun intercept(chain: Interceptor.Chain): Response {
            val request:Request = chain.request().newBuilder().addHeader("Authorization",
                "Bearer "+customSharedPreferences.getStringFromSharedPreferences(APP_TOKEN_KEY).trim()).build();
            return chain.proceed(request);
        }
    }).build();
}

@Singleton
@Provides
fun provideParametreDao(database: KutuphanemDatabase) = database.getParametreDao();

@Singleton
@Provides
fun provideParametreApi(retrofit: Retrofit):IParametreService = retrofit.create(IParametreService::class.java);

@Singleton
@Provides
fun provideKullaniciApi(retrofit: Retrofit):KullaniciService = retrofit.create(KullaniciService::class.java);

@Singleton
@Provides
fun provideKitapApi(retrofit: Retrofit):IKitapService = retrofit.create(IKitapService::class.java);

@Singleton
@Provides
fun provideKullaniciDao(database: KutuphanemDatabase) = database.getKullaniciDao();

}

When I rotate the screen the inputs are losing. How to solve this problem?

4
  • Are you talking about views losing their state? If so then your view model isn't relevant (at least with the code you've provided). Commented Apr 18, 2021 at 20:59
  • Yes , it is relevant with losing state.I think it caused by initialize view model. I edited the question with adding module. Commented Apr 18, 2021 at 21:03
  • You haven't mentioned what exactly you're losing? I'm not sure what your Retrofit dependency etc has to do with it? Commented Apr 18, 2021 at 21:53
  • I am losing form data. Firstly the form is filling by service. Then I am changing a date.Last when I rotate the screen , data is again default value that come from service. Commented Apr 19, 2021 at 6:24

1 Answer 1

0

I found the error. It is not relevant my view model or injections. It caused by two way databinding. But even so I put this line into the contructor of viewmodel;

 private val savedStateHandle: SavedStateHandle

I am controlling the state with this param.So my inputs are not losing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.