Activity shows before moving to another activity while using datastore to check for saved preferences

446 Views Asked by At

I am using the new DataStore to save user Preferences in Android App.

I am trying to show an intro activity for first time users.

Problem - After saving the preference using data store, I then check to see if there is a saved preference already - If yes - Move to the next activity. The problem is that the Intro Activity shows slightly before moving to the next activity which is definitely a bad UI/UX.

This doesn't happen when I used SharedPreferences.

IntroActivity

 private var hasSeenIntro: Boolean = true 
 .......

 //I also used a splash screen, but the activity still shows slightly.
 setTheme(R.style.AppTheme_NoActionBar);
 super.onCreate(savedInstanceState)


  dataManager.hasSeenIntro.asLiveData().observe(this){
        hasSeenIntro = it
        if(hasSeenIntro){
            val intent = Intent(this, RegisterOrLoginActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivity(intent)
            finish()
        }
    }

DataManager

private val dataStore = context.createDataStore("data_prefs")

companion object{
    val HAS_SEEN_INTRO = preferencesKey<Boolean>("HAS_SEEN_INTRO")
}

suspend fun storeHasSeenIntro(intro: Boolean){
    dataStore.edit {data->
        data[HAS_SEEN_INTRO] = intro
    }
}

val hasSeenIntro : Flow<Boolean> = dataStore.data.map {
    it[HAS_SEEN_INTRO] ?: false
}
0

There are 0 best solutions below