Android Jetpack Compose variables are reset to default values

Viewed 32

hello everyone, I need help.

When launching the application:

  1. A user authorization check is performed in the application
  2. If the user is authorized, then the initialization module in the application is launched, which performs preparatory procedures, including subtracting the user profile to the repository variable
@Singleton
class RepProfiles @Inject constructor(
    private val repFB: RepFB,
    private val repApp: RepApp
){
    var profileUser: ProfileUser = ProfileUser()
    ...
  1. Next, the user profile is displayed in LeftDrawer from the global variable
@HiltViewModel
class VmLeftDrawerApp
@Inject constructor(
    val repFB: RepFB,
    val repProfiles: RepProfiles,
    val modInitApp: ModInitApp
) : ViewModel()

val viewModel = hiltViewModel<VmLeftDrawerApp>()

Text(
    modifier = Modifier.paddingFromBaseline(top = 20.dp),
    text = viewModel.repProfiles.profileUser.nameShow
)

Now, I minimize the application for a couple of days and, apparently, the operating system does something with it, and when restoring, when opening LeftDrawer, the profile field is empty, that is, it seems that variables are initialized, as when the application is first launched.

I tried to make an option with a cache, that is, to store a class with a profile in the device memory. It works, but I didn't like the following:

  1. When opening LeftDrawer, all static elements have already been displayed, and after a second the value from the profile appears. It's somehow ugly, but it seems ugly to me to make a progress bar on LeftDrawer.
  2. It turns out that for each similar variable you need to make your own cache. It is time-consuming and it is problematic to track which ones are needed and which ones are not.

Next, I thought that it would suit me to somehow determine that the variables were reset and start the initialization module in the application over again, that is, start the application almost from the beginning, except for the authorization check. I check in onResume if the profile variable is reset (empty string), then run the initialization module. Jetpack Compose doesn't have a convenient mechanism for working with the lifecycle, so I came up with a way with LiveData, but unfortunately, it hasn't worked yet and we need to investigate further. Complicating the fact that the experiment takes at least a day to wait for the operating system to reset the variables.

    override fun onResume() {
        super.onResume()

        if (repProfiles.profileUser.uidFbUser.isEmpty()) {
            callMain.value = Calendar.getInstance().time.time.toString()
        }
    }
@Composable
fun Main() {

    val timestampCall = callMain.observeAsState()
    var recompose = timestampCall.value

    Navigation()
}

Maybe someone has other ideas or practices for storing a profile in a database and working with it? The feeling that I'm coming up with a bike should be a popular solution.

0 Answers
Related