Skip to content

Commit 76e4506

Browse files
committed
Fix bug where the switch wasn't set correctly
I don't know if this bug occurs on any other devices, but on my Nexus 5X the switch in the app bar is set incorrectly after the following steps: 1. Open Red Moon 2. Start the filter 3. Use the home button to exit Red Moon 4. Stop the filter through the notification 5. Reopen Red Moon through the launcher After these steps the switch in the activity is still on for me, although the filter is off. To fix this, I've added an attempt to set the switch correctly on the Resume event. Since the Switch may not be initialized (if the app is opened after closing completely), we have to catch the NotInitializedException. As far as I've found, there is no other way to check if a lateinit variable is initialized in Kotlin. This fix may be a bit crude, but I don't think it does any harm, and it does fix the bug at least on my phone.
1 parent 9462134 commit 76e4506

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

app/src/main/java/com/jmstudios/redmoon/activity/MainActivity.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ class MainActivity : ThemedAppCompatActivity() {
112112
super.onResume()
113113
// The switch is null here, so we can't set its position directly.
114114
invalidateOptionsMenu()
115+
116+
// Attempt to set the Switch correctly, because at least on my device, it doesn't have the
117+
// correct state when:
118+
// 1 Open Red Moon
119+
// 2 Start the filter
120+
// 3 Use the home button to exit Red Moon
121+
// 4 Stop the filter through the notification
122+
// 5 Reopen Red Moon through the launcher
123+
// After these steps the switch in the activity is still on for me, although the filter is
124+
// off.
125+
try {
126+
// Try to update the position of the switch, if it has already been created
127+
mSwitch?.isChecked = Config.filterIsOn;
128+
} catch (e: Exception) {
129+
// Apparently the switch wasn't initialized yet, so it will be set correctly when it is
130+
// initialized.
131+
}
132+
115133
EventBus.register(this)
116134
}
117135

0 commit comments

Comments
 (0)