Bug with Set Slider.thumbPosition

set Slider1.ThumbPosition always triggers a Slider1.PositionChanged event even if the thumbPosition isn't changed. This has the potential to cause many excess events, and I suspect it already is in many projects.

Example: A slider being used as a progress meter that is updated periodically. If progress stalls, the thumbPosition will be set to the same value multiple times mistakenly causing repeated PositionChanged events when nothing has changed.

An example of a worst-case scenario: Make a rookie mistake and set thumbPosition to the thumbPosition parameter within the PositionChanged event and the app user sees a memory stack overflow error.

A global prevPosition variable could be implemented to avoid these, but that is complexity that the name of the event indicates has already been abstracted away.

Yes, the event will be triggered if you set the thumb position to something, no matter if it is different or not

As workaroud use an if statement and set the new thumb position only if it really changed

Taifun

Yeah, that is a workaround, but it requires additional code that the name of the event indicates has already been abstracted away.

I put a pull request on github a while back with a fix for this weird slider behavior, with a few more improvements. I even added an implementation for iOS. Unfortunately it's still at a standstill.

1 Like

@Patryk_F Thank you for your contributions!! Please excuse my ignorance... I looked over the PR and I'm not sure it impacts when set ThumbPosition is used and the ThumbPosition doesn't change.

Yes, I didn't describe this problem in the PR because it seemed trivial to me. The problem you describe is a bit more complex. The ProgressBar android view, on which SeekBar is based, and in turn the Slider AppInventor component is based, does not actually trigger the onChanged event when the value being set is the same as before. This was added in the Slider implementation.

Maybe there would be nothing wrong with it if not for the fact that:

  • when we set the thumb value to the same value as before, the event is triggered once (from the Slider implementation),

  • when we set a different value than before, the event is triggered twice (once from the Slider implementation and once from the ChangeListener of the android view).

Below is a simple project that shows this problem:

sliderBug.aia (2.3 KB)

First progress settings, the value is different, so the event is triggered twice:

Second progress settings, the value is different, so the event is triggered once:

Yes, I have observed this behavior, but ignorantly shrugged it off. It does sound like an issue.

That history and this documentation don't make sense to me.

The event name will remain as is, but perhaps the help could be change to: "Indicates that the position of the slider thumb has been set."

I love that you have contributed TouchUp and TouchDown events for the Slider. Is it plausible to add a boolean changedByTouch parameter to the existing PositionChanged event? This might also help with some of these tricky situations.

We can't do this because it would break projects that use this event. But actually having TouchDown and TouchUp events, this isn't necessary because you can make your own boolean variable that is set to true in TouchDown and false in TouchUp. I think the internal implementation of the SeekBar view does the same thing.

You maybe want to use an extension like [FREE / Open Source] SliderTools - Bit of useful feature for sliders if you are not working on an iOS app.
there is a fromUser param in Changed event.

@Kevinkun Thanks for the suggestion. I originally checked out SliderTools, but I cannot exclude iOS :face_with_diagonal_mouth:

then you can draw a slider in canvas.