forked from osfans/trime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lifecycle): enhance lifecycle management
- Loading branch information
1 parent
cd018f8
commit a4290fa
Showing
4 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/osfans/trime/ime/lifecycle/CoroutineScopeJava.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.osfans.trime.ime.lifecycle | ||
|
||
import kotlinx.coroutines.MainScope | ||
|
||
object CoroutineScopeJava { | ||
@JvmStatic | ||
val MainScopeJava | ||
get() = MainScope() | ||
} |
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/osfans/trime/ime/lifecycle/LifecycleInputMethodService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.osfans.trime.ime.lifecycle | ||
|
||
import android.inputmethodservice.InputMethodService | ||
import android.view.View | ||
import androidx.annotation.CallSuper | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.coroutineScope | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.LifecycleRegistry | ||
import androidx.lifecycle.ViewTreeLifecycleOwner | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
open class LifecycleInputMethodService: InputMethodService(), | ||
LifecycleOwner | ||
{ | ||
private val lifecycleRegistry by lazy { LifecycleRegistry(this) } | ||
|
||
val uiScope: CoroutineScope | ||
get() = lifecycle.coroutineScope | ||
|
||
final override fun getLifecycle(): Lifecycle { | ||
return lifecycleRegistry | ||
} | ||
|
||
@CallSuper | ||
override fun onCreate() { | ||
super.onCreate() | ||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) | ||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START) | ||
} | ||
|
||
@CallSuper | ||
override fun onCreateInputView(): View? { | ||
val decorView = window!!.window!!.decorView | ||
ViewTreeLifecycleOwner.set(decorView, this) | ||
return null | ||
} | ||
|
||
@CallSuper | ||
override fun onDestroy() { | ||
super.onDestroy() | ||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP) | ||
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) | ||
} | ||
} |