Skip to main content
1 of 2

Logic to check if app was launched for the first time using DataStore

I have written a dummy code to check if an app was launched for the time using DataStore. It is working however i want to know if this can be optimised and i also observed a small glitch initially like black screen for like a few seconds only for the first installation. I would like to know why thats being caused as well.

MainActivity


class MainActivity : ComponentActivity() {
    private lateinit var dataStore: FirstTimeLaunch
    private lateinit var viewModel : FirstTimeViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = ViewModelProvider(this)[FirstTimeViewModel::class.java]
        dataStore = FirstTimeLaunch(this)
        setContent { 
            MyApp(viewModel = viewModel)
        }

    }

    @Composable
    private fun MyApp(viewModel: FirstTimeViewModel) {
        val isFirstTime by viewModel.getFirstTime.observeAsState(initial = true)

        Surface(color = MaterialTheme.colorScheme.onPrimary) {
            Column(
                modifier = Modifier.fillMaxSize(),
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center
            ) {
                if (isFirstTime) {
                    FirstTimeContent(viewModel)
                } else {
                    NonFirstTimeContent()
                }
            }
        }
    }

    @Composable
    private fun FirstTimeContent(viewModel: FirstTimeViewModel) {
        Column(
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(text = "First Time Launch")
            Button(onClick = { viewModel.setFirstTime(false) }) {
                Text(text = "Mark as Not First Time")
            }
        }
    }

    @Composable
    private fun NonFirstTimeContent() {
        Text(text = "Non First Time Launch")
    }

FirstTimeViewModel

class FirstTimeViewModel(application: Application) : AndroidViewModel(application) {

    val dataStore = FirstTimeLaunch(application)
    val getFirstTime = dataStore.getFirstTimeLaunch().asLiveData(Dispatchers.IO)

    fun setFirstTime(isFirstTime: Boolean){
        viewModelScope.launch {
            dataStore.setFirstTime(isFirstTime)
        }
    }
}

FirstTimeLaunch

class FirstTimeLaunch (context : Context){

    private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "FirstTimeLaunch")
    private val dataStore = context.dataStore

    companion object{
        val IS_FIRST_TIME_LAUNCH_KEY = booleanPreferencesKey("isFirsTime")
    }

    suspend fun setFirstTime(isFirstTime : Boolean){
        dataStore.edit {
            preferences -> preferences[IS_FIRST_TIME_LAUNCH_KEY] = isFirstTime
        }
    }

    fun getFirstTimeLaunch(): Flow<Boolean> {
        return dataStore.data.map {
            pref -> pref[IS_FIRST_TIME_LAUNCH_KEY]?: true
        }
    }

}

```