Skip to content

Fix WebView workaround causing Activity to be shown with incorrect theme#5844

Merged
seadowg merged 1 commit intogetodk:v2023.3.xfrom
seadowg:fix-theme
Nov 23, 2023
Merged

Fix WebView workaround causing Activity to be shown with incorrect theme#5844
seadowg merged 1 commit intogetodk:v2023.3.xfrom
seadowg:fix-theme

Conversation

@seadowg
Copy link
Member

@seadowg seadowg commented Nov 22, 2023

This fixes an issue where form entry starts with the dark/light theme when the other is selected. To reproduce using v2023.3.0:

  1. Set the project to use the opposite of the system (i.e. light if the system is dark)
  2. Kill the app
  3. Reopen the app and start a form

You'll see the first screen loads as the system theme in some parts, and the project theme in others.

Why is this the best possible solution? Were any other approaches considered?

We were previously avoiding a very similar issue from occurring if the image select widget was in a form: once the widget was viewed, moving backwards or forwards in a form would cause some parts of the view to use the system theme rather than the project one. See this Android issue for more details.

The solution here is mostly described in my comment in FixMismatchThemeProblemsActivity:

When the app's theme doesn't match the system, the first [WebView] created will cause the host Activity to be recreated with incorrect resource loading (the system ones instead of the custom set dark/light resources). This doesn't happen for subsequent [WebView] creations however.

This Activity can be started at launch to be "sacrificed" so no other Activity has to deal with the problem.

How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?

Should just fix the issue as described. It would be good to try and mess around with different projects using different themes to see if there's still a way to reproduce.

The problems the WebView was actually dealing with was that if you opened a form with a select one from image widget, moved to it and then moved to another widget (backwards or forwards), parts of the UI would use the system theme rather than the app one. You can see conversations about that here.

Before submitting this PR, please make sure you have:

  • added or modified tests for any new or changed behavior
  • run ./gradlew checkAll and confirmed all checks still pass OR confirm CircleCI build passes and run ./gradlew connectedDebugAndroidTest locally.
  • added a comment above any new strings describing it for translators
  • verified that any code or assets from external sources are properly credited in comments and/or in the about file.
  • verified that any new UI elements use theme colors. UI Components Style guidelines

@seadowg seadowg changed the base branch from master to v2023.3.x November 22, 2023 16:57
@seadowg seadowg changed the title Fix WebView workaround causing Activity to be shown with incorrect theme Fix WebView workaround causing Activity to be shown with incorrect theme Nov 22, 2023
@seadowg seadowg added this to the v2023.3.x milestone Nov 22, 2023
@seadowg seadowg requested a review from grzesiek2010 November 22, 2023 17:03
@seadowg seadowg marked this pull request as ready for review November 22, 2023 17:03
@lognaturel lognaturel added the high priority Should be looked at before other PRs/issues label Nov 22, 2023
*
* See [this issue](https://issuetracker.google.com/issues/37124582) for more details.
*/
class FixMismatchThemeProblemsActivity : AppCompatActivity() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it really need to be in an activity? Maybe it could be just a method in ApplicationInitializer instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah great point! I'd assumed that we needed an Activity to "sacrifice", but it looks like if I stick the WebView initialisation in ApplicationInitializer it does fix the problems. Will rework this.

@seadowg seadowg marked this pull request as draft November 23, 2023 10:24
@seadowg seadowg marked this pull request as ready for review November 23, 2023 11:23
@seadowg seadowg requested a review from grzesiek2010 November 23, 2023 11:23
@srujner
Copy link

srujner commented Nov 23, 2023

Tested with Success!

Verified on device with Android 13

Verified cases:

  • Issue is no longer reproducing;
  • Switching between light and dark mode;
  • Light and dark mode set in device and the opposite in the app;
  • Switching between projects with different themes;
  • Don't keep activities enabled/disabled

@dbemke
Copy link

dbemke commented Nov 23, 2023

Tested with Success!

Verified on device with Android 10

@seadowg seadowg merged commit 86fe8ad into getodk:v2023.3.x Nov 23, 2023
@seadowg seadowg deleted the fix-theme branch November 23, 2023 15:58
seadowg added a commit to seadowg/collect that referenced this pull request Nov 30, 2023
Fix `WebView` workaround causing Activity to be shown with incorrect theme
seadowg added a commit to seadowg/collect that referenced this pull request Dec 7, 2023
Fix `WebView` workaround causing Activity to be shown with incorrect theme
@seadowg seadowg mentioned this pull request Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

behavior verified high priority Should be looked at before other PRs/issues

Projects

Status: done

Development

Successfully merging this pull request may close these issues.

5 participants