Skip to content

fix: prevent crash on _activeTheme access before ThemeManager initialization - #2070

Draft
Bambooin with Copilot wants to merge 1 commit into
developfrom
copilot/fix-crash-when-switching-to-trime
Draft

fix: prevent crash on _activeTheme access before ThemeManager initialization#2070
Bambooin with Copilot wants to merge 1 commit into
developfrom
copilot/fix-crash-when-switching-to-trime

Conversation

Copilot AI commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

After prolonged inactivity, Android may call onCreateInputView() before ThemeManager.init() completes — the init runs in a coroutine suspended until Rime is ready, creating a race where _activeTheme (lateinit) is accessed uninitialized, crashing with UninitializedPropertyAccessException.

Changes

  • ThemeManager — expose isInitialized: Boolean via ::_activeTheme.isInitialized to allow safe pre-init checks

  • TrimeInputMethodService — add inputViewPending: Boolean flag; guard onCreateInputView() to bail early (returning null) if theme isn't ready yet, and after ThemeManager.init() completes in the async Rime-ready block, trigger deferred replaceInputViews() if the flag is set:

override fun onCreateInputView(): View? {
    if (!ThemeManager.isInitialized) {
        inputViewPending = true
        return null
    }
    replaceInputViews(ThemeManager.activeTheme)
    return null
}

// inside rime.runOnReady { ... }
ThemeManager.init(resources.configuration)
ThemeManager.addOnChangedListener(onThemeChangeListener)
ColorManager.addOnChangedListener(onColorChangeListener)
if (inputViewPending) {
    inputViewPending = false
    replaceInputViews(ThemeManager.activeTheme)
}

Copilot AI linked an issue Aug 11, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix crash when switching to trime due to uninitialized _activeTheme fix: prevent crash on _activeTheme access before ThemeManager initialization Aug 11, 2026
Copilot AI requested a review from Bambooin August 11, 2026 13:06
@Bambooin
Bambooin requested a lite review from Copilot August 11, 2026 13:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Prevents an UninitializedPropertyAccessException crash when Android calls onCreateInputView() before ThemeManager.init() finishes (e.g., after long inactivity), by adding an explicit initialization check and deferring input-view creation until the theme is ready.

Changes:

  • Add ThemeManager.isInitialized to safely detect whether _activeTheme has been set.
  • Guard TrimeInputMethodService.onCreateInputView() to return early when the theme isn’t ready and set a pending flag.
  • After ThemeManager.init() completes in the Rime-ready block, trigger a deferred replaceInputViews() if input view creation was previously requested.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
app/src/main/java/com/osfans/trime/ime/core/TrimeInputMethodService.kt Defers input-view creation until theme initialization completes, then replays the creation if needed.
app/src/main/java/com/osfans/trime/data/theme/ThemeManager.kt Exposes a safe isInitialized check for _activeTheme to avoid lateinit access crashes.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

When the IME is reattached after being idle for a long time, Android
may call onCreateInputView() before ThemeManager.init() completes
(which runs asynchronously after Rime is ready). This caused an
UninitializedPropertyAccessException on _activeTheme.

Fix: Add ThemeManager.isInitialized property, check it in
onCreateInputView() and defer view creation via inputViewPending flag.
After ThemeManager.init() completes, create input views if pending.

Closes #2058

Co-authored-by: Bambooin <7157227+Bambooin@users.noreply.github.com>
@Bambooin
Bambooin force-pushed the copilot/fix-crash-when-switching-to-trime branch from 5dedf1e to 577b718 Compare August 11, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

切换到 trime 时奔溃:_activeTheme 未初始化

3 participants