Reputation: 357
I have a View with multiple progress bars.Individual progress bars doesnt persist their value when i switch to last tab(tab 4) and i come back.[Basically when home tab is not visible and brought to visibility again].
The progress bars are placed inside a LinearLayout which is hosted inside Fragment. Looks like Youtube app. In home page, i have 5 progress bar. For the first time when i set progress all progress bar values looks perfect. Individual progress bar are showing their own individual values.
However when i switch to tab 4(not in picture), i come back, all progress bar's value get set to the last progress bar's value. See Image below.
I checked by removing Progress bar with TextBox, the value persists as per set. I have added max value to progress bar, used the methods 1. setProgress, 2. incrementProgressBy, no improvement.
Since textbox behaves correctly ,i couldnt figure out my problem kindly help.
PS: In android Fragment(tab) gets recreated when its not visible and brought to visibility.to avoid this i made restrictions in onCreateview to persist the last created rootview.
Upvotes: 1
Views: 1624
Reputation: 389
There is an accepted answer, but I ran into this same issue with multiple progress bars on different Fragments and in recycler views. My issue was related to a well observed Android progress bar bug that does not always update progress bars (see accepted answer here: android progressBar does not update progress view/drawable)
In my case I had a custom Progress Bar and added these methods to get around this bug. Sucks that we as developers have to figure this out the hard way so often.
fun setProgressSafely(progress: Int) {
setProgress(progress + 1)
setProgress(progress)
}
fun setMaxSafely(max: Int) {
setMax(max + 1)
setMax(max)
}
Upvotes: -1
Reputation: 745
I had the same problem with 2 progressBars inside a ViewPager and fixed by using a unique drawable instance for each progressBar:
This shows wrong values on progressBar:
val progressDrawable = ContextCompat.getDrawable(it, R.drawable.progress_bar_colors)
mBinding.firstProgressBar.progressDrawable = progressDrawable
mBinding.secondProgressBar.progressDrawable = progressDrawable
And this fixed the problem:
mBinding.firstProgressBar.progressDrawable = ContextCompat.getDrawable(it, R.drawable.progress_bar_colors)
mBinding.secondProgressBar.progressDrawable = ContextCompat.getDrawable(it, R.drawable.progress_bar_colors)
I hope it helps you
Upvotes: 2
Reputation: 536
You probably have solved this by now, but in case anyone else gets to this page with the same issue: I had the same problem, multiple progressbars in fragments in a tabbed container view (ViewPager), and all of them showing the same progress value (the one last set) after switching between tabs. I solved it by setting the progress value in the fragment's onResume
instead of onCreateView
.
Upvotes: 2