Recently I came across an issue when I tried to set the progress for the ProgressBar after I pressed the Power button. I'm using the onSaveInstanceState method to save all information needed and then I restore the values in onCreate. To simplify the issue I am using hardcoded values when restoring the values.
When I initialise the values for the ProgressBar everything works as expected, however when I press the Power button and then press the Power button again to wake up the (app) Activity the ProgressBar shows 0 progress in the Android Studio Layout inspector. Interestingly the Android Studio Logcat shows the correct information.
What I tried to do so far: I checked that I have a connection to the correct ProgressBar by setVisibility for the ProgressBar GONE and it is working. I tried to invalidate the ProgressBar and other views without luck.
My first question: How to set and show the progress in ProgressBar correctly after the app wakes up?
My Second question: Why my solution does not work when I set the values as hardcoded?
onCreate
...
if (savedInstanceState != null) {
restoreInstanceStateForProgressBar(savedInstanceState);
} else {
initProgressBarForEndSession( "Title", "Message",
"Progress Bar 1", FOURTH_PROGRESS_BAR_MAX, fourthProgressBarProgress,
"Progress Bar 2", FIFTH_PROGRESS_BAR_MAX, fifthProgressBarProgress
);
}
...
private void restoreInstanceStateForProgressBar(@NonNull Bundle _savedInstanceState) {
try {
binding.includeProgressBarEndSession.clRoot.setVisibility(View.VISIBLE);
ConstraintLayout clRoot = (ConstraintLayout)
findViewById(R.id.includeProgressBarEndSession);
if (clRoot != null) {
int clRootChildrenCount = clRoot.getChildCount();
View rootTitleView = clRoot.getChildAt(0);
if (rootTitleView instanceof TextView) {
TextView rootTitle = (TextView)
clRoot.getChildAt(0);
rootTitle.setText("Title");
}
View rootMessageView = clRoot.getChildAt(1);
if (rootMessageView instanceof TextView) {
/*
assign the value
*/
TextView rootMessage = ((TextView)
clRoot.getChildAt(1));
rootMessage.setText("Message");
}
}
ConstraintLayout constraintLayout = (ConstraintLayout)
findViewById(R.id.clRootPb);
if (constraintLayout != null) {
int clChildrenCount =
constraintLayout.getChildCount();
for (int i = 0; i < clChildrenCount; i++) {
View incProgressBarView =
constraintLayout.getChildAt(i);
if (incProgressBarView instanceof
ConstraintLayout) {
ConstraintLayout incProgressBar =
(ConstraintLayout)
constraintLayout.getChildAt(i);
if (i == 3 || i == 4) {
incProgressBar.setVisibility(View.VISIBLE);
View tvPbTitleView =
incProgressBar.getChildAt(0);
if (tvPbTitleView instanceof TextView)
{
TextView tvPbTitle = (TextView)
incProgressBar.getChildAt(0);
tvPbTitle.setText("tvPbTitle");
}
View pbView =
incProgressBar.getChildAt(1);
if (pbView instanceof ProgressBar) {
ProgressBar pb = (ProgressBar)
incProgressBar.getChildAt(1);
pb.setProgress(100);
System.out.println("\nTAG" +
"\nrestoreInstanceStateForProgressBar" + "" +
"\nProgressBar Visibility: " + pb.getVisibility() + "" +
"\nProgressBar Max: " + pb.getMax() + "" +
"\nProgressBar Progress: " + pb.getProgress());
/*
test set View.GONE for the
ProgressBar
*/
// pb.setVisibility(View.GONE);
}
View tvPbPercentageView =
incProgressBar.getChildAt(2);
if (tvPbPercentageView instanceof
TextView) {
TextView tvPbPercentage =
(TextView)
incProgressBar.getChildAt(2);
tvPbPercentage.setText("...%");
}
View tvPbDoneView =
incProgressBar.getChildAt(3);
if (tvPbDoneView instanceof TextView) {
TextView tvPbDone = (TextView)
incProgressBar.getChildAt(3);
tvPbDone.setText("...of...");
}
}
}
}
}
} catch (Exception ex) {
}
}
Layout Inspector before Power button pressed:
Layout Inspector after wake up:

Logcat after wake up:

