I'm using something like this ```backButton.setOnL...
# announcements
z
I'm using something like this
Copy code
backButton.setOnLongClickListener {
    GlobalScope.launch(Dispatchers.Main) {
        if (backButton.isPressed) {
            viewPager.setCurrentItem(viewPager.currentItem - 1, true)
            delay(500)
        }
    }
    true
}
g
Yep, this approach will work, but do not use global scope, use lifecycleScope instead Also makes sense to move isPressed check outside of launch to avoid starting useless coroutine
I would also recommend to create an extension function which encapsulates this code so it will be a lot easier to use in your code
z
@gildor Honestly the coroutine version, I'm using is not working, not sure why, maybe I have to use a job to do this part
Copy code
handler.postDelayed(runnable, 0)
g
What is not working? I'm not quite sure what you trying to achieve with those delays though
What you want to achieve and why you need post delayed 0 in original code
Ah, got it, you want to repeat it again in 500 Ms, right? Just move delay before setCurrent item and duplicate isPresed check before you run coroutines
z
What I want to achieve? As long as the button is pressed do some work. Using delay to slow the output
g
onClick { If (isPressed) return lifecycleScope.launch { delay(500) if (isPressed) return setItem() } }
Slow output? What do you mean? I see that old code removes listener, so you want to prevent additional clicks, right?
z
By slow output I meant that as long as user is pressing the button I have to do some work in the main thread, just need to add a delay to it, for a change in the user experience.
g
So looks that my example above will work, you just should delay before doing work, not after
z
okay let me just check, but I'm not sure about the return part
If (isPressed) return
g
Sorry, it's inverted in original code, so it's if (!isPressed)
The same as your original code, just an early return instead
z
I think though the your solution might be working but, I'm unable to apply it on my end.
g
Could you show your code
g
This is your original code, just curious what is not working with your current Kotlin implementation
z
It runs only once, so I've read somewhere the launch means we are creating a new coroutine and as it is light weighted we can create as such much as we want
g
Runs only once? So it works only on first long click?
Yeah, no issue to create a coroutine on every click, not much different comparing to your java code with handler
z
Yes just on the first click
d
I think you want a while loop?
Nvm, I'll answer your stack overflow question instead. I'd like some points.
💯 1
Answered
⏸️ 1
z
Hey @Dominaezzz checking your solution
But @Dominaezzz can you give me some explanation for why using while loop worked over here?
d
Your old code was looping too, just using the dispatcher instead.
z
Okay understood, thank you.
@Dominaezzz In the thread @gildor mentioned about to create extension function out of it any suggestions on it?
I'm thinking of using FlowBinding https://github.com/ReactiveCircus/FlowBinding
d
I don't have strong opinions on this. Up to you I think.