-1

I'm trying to create a pop-up dialog that only displays after the first launch of the app, notifying the user of the app's consent to receive notifications. So I have a dialog like this

class MainActivity : BaseActivity() {

    private var agreement = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)           
        if (!agreement) {
            val notifyBottomDialogFragment: BottomNotifyFragment = BottomNotifyFragment {
                when (it) {
                    0 -> agreement = true
                    1 -> agreement = false
                }
            }
            notifyBottomDialogFragment.show(supportFragmentManager, notifyBottomDialogFragment.tag)
            notifyBottomDialogFragment.isCancelable = false

        } else {
            Log.d("test", "not first")
        }

However, if I switch back to the main activity from another activity, the dialog will still be displayed. How can I solve this problem, anyone can help?

Daesik Park
  • 111
  • 15
  • Does this answer your question? [Android Shared preferences for creating one time activity (example)](https://stackoverflow.com/questions/23024831/android-shared-preferences-for-creating-one-time-activity-example) – a_local_nobody Dec 09 '20 at 10:01
  • Everytime you create MainActivity you assign false to agreement. For example if you hit home button activity is destroyed. So The solution is to save the value with shared preferences as @a_local_nobody mentioned – F.Mysir Dec 09 '20 at 10:25

3 Answers3

0

Read/write agreement value to SharedPreferences. This way it will persist between app launches.

https://stackoverflow.com/a/23024962

Solidogen
  • 364
  • 3
  • 9
0

You should close your dialog in your onPause or before moving to the next activity.

You should create object for BottomNotifyFragment globally to access it in another function.

 var notifyBottomDialogFragment: BottomNotifyFragment? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!agreement) {
            notifyBottomDialogFragment = BottomNotifyFragment {
                when (it) {
                    0 -> agreement = true
                    1 -> agreement = false
                }
            }
            notifyBottomDialogFragment.show(supportFragmentManager, notifyBottomDialogFragment.tag)
            notifyBottomDialogFragment.isCancelable = false

        } else {
            Log.d("test", "not first")
        }

    }

    override fun onPause() {
        super.onPause()
        if(null!=notifyBottomDialogFragment){
            notifyBottomDialogFragment.close()
        }
    }
MathankumarK
  • 1,987
  • 11
  • 26
0

As people have said, you need to store the value somehow, to persist between app runs, and SharedPreferences is the way to go. You can have a separate file for things like app state if you like, to keep it distinct from things like user preferences (although in this case maybe that's exactly what it is)

context.getSharedPreferences(APP_STATE_PREFERENCES_NAME, Context.MODE_PRIVATE)

Whenever the app runs, you need to check if the user has consented, e.g. in MainActivity. That means checking the SharedPreferences before deciding whether to display the consent dialog or not.

The important thing here is that you basically have three states - consent given, consent denied, and undefined. The first two mean the user has made a decision, and you shouldn't show the dialog. The last one means they haven't confirmed or denied yet, so it needs to be displayed.

Because the value you're storing is a boolean, you can't use the getBoolean method with a default value, because undefined would look the same as consent denied - so you need to use the contains method to check for a preference with that key. If it doesn't exist, you haven't stored a consent value yet, so you need to pop the dialog. Once they make a decision, store it. This also gets around e.g. closing the app when the popup appears - nothing's stored, so it pops back up next time

cactustictacs
  • 4,293
  • 1
  • 4
  • 13