0

In some project we use Navigation Architecture Component, in navigation graph we added Fragment with global action, but issue is when quickly click multiple times on button after which navigate to Fragment - Fragment opens multiple times.

I have some options to solve this issue:

  1. class SafeClickListener(
                private var defaultIntervalMs: Long = 1000,
                private val onSafeCLick: (View) -> Unit
        ) : View.OnClickListener {
    
                private var lastTimeClicked: Long = 0
    
                override fun onClick(v: View) {
                    if (SystemClock.elapsedRealtime() - lastTimeClicked < defaultIntervalMs) {
                        return
                    }
                    lastTimeClicked = SystemClock.elapsedRealtime()
                    onSafeCLick(v)
                }
        }
    
  2. use app:launchSingleTop="true" on global action, but still we see how multiple Fragments opens (this solution only solved popBackStack problem)

  3. Use custom fragment navigator where we e.g add custom attribute 'mustBeOnce' to Destination and in overrided method navigate() we will check if this destination already in our backstack.

May be who can suppose other solutions?...

kovac777
  • 730
  • 6
  • 19

1 Answers1

0

If you want to avoid accidental clicks then you must disable the button after calling the Clicklistener. And after when you return to the previous screen you must to enable the button in onResume() method.