Has anyone tried to show some compose UI for when ...
# compose
j
Has anyone tried to show some compose UI for when the app was moved to the background? The use case is that we need to have a privacy screen that will be shown in the app switcher whenever the user has closed the app. I was able to achieve a UI change by updating a
mutableStateOf
when the activity hit
onPause
but then in the app switcher it still showed the old UI. I also was trying to use the Lifecycle architecture component to update the state but that didn’t work either. Is this something that can be achieved with Compose? The current code is in the thread.
Copy code
@AndroidEntryPoint
class MainActivity : AppCompatActivity(), LifecycleObserver {
    private val termsViewModel by viewModels<TermsOfUseViewModel>()
    private val privacyViewModel by viewModels<PrivacyViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ProcessLifecycleOwner.get().lifecycle.addObserver(this)

        setContent {
            when {
                privacyViewModel.showPrivacyScreen -> {
                    PrivacyScreen()
                }
                !termsViewModel.hasAcceptedTermsOfUse -> {
                    TermsOfUseScreen(acceptTermsOfUse = { termsViewModel.acceptTermsOfUse() })
                    
                }
                else -> {
                    ComposeApp(applicationContext)
                } 
            }
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onAppForegrounded() {
        privacyViewModel.updateShowPrivacyScreen(false)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onAppBackgrounded() {
        privacyViewModel.updateShowPrivacyScreen(true)
    }
}

@HiltViewModel
class PrivacyViewModel
@Inject
constructor(
    val appContext: BaseApplication,
    private val sharedPreferences: SharedPreferences
) : ViewModel() {
    private val _showPrivacyScreen: Boolean
        get() {
            val shouldShowPrivacyScreen = sharedPreferences.getBoolean(
                appContext.getString(R.string.should_show_privacy_screen),
                false
            )

            return shouldShowPrivacyScreen
        }
    var showPrivacyScreen by mutableStateOf(_showPrivacyScreen)

    fun updateShowPrivacyScreen(value: Boolean) {
        showPrivacyScreen = value

        with(sharedPreferences.edit()) {
            putBoolean(
                appContext.getString(R.string.should_show_privacy_screen),
                value
            )
            
            apply()
        }
    }
}
Well I found this:
Copy code
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
which at least gives the security measure needed but it doesn’t look nice. I would really like to be able to show a custom UI that can hide the sensitive content.
a
AFAIK you can't show a custom snapshot except completely disable it.
z
You’d probably want to use flag_secure anyway, since it also does other privacy stuff like block screenshots while you’re in the foreground, prevent rendering on insecure displays
☝🏻 1
j
Ah, that makes sense. Cool, thank you!