Created
April 4, 2019 14:22
-
-
Save featzima/2567135cbaff227a69453d7bf1e5269a to your computer and use it in GitHub Desktop.
FragmentSoftInputCorrector changes a soft input type of activity for a single fragment that requires SOFT_INPUT_ADJUST_RESIZE during it is attached to this activity
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
/** | |
* FragmentSoftInputCorrector changes a soft input type of activity | |
* for a single fragment that requires SOFT_INPUT_ADJUST_RESIZE | |
* during it is attached to this activity | |
* | |
* Usage inside a fragment: | |
* | |
* override fun onAttach(context: Context) { | |
* super.onAttach(context) | |
* FragmentSoftInputCorrector(activity!!, this) | |
* } | |
*/ | |
class FragmentSoftInputCorrector @Inject constructor( | |
private val activity: FragmentActivity, | |
private val fragment: Fragment) : LifecycleObserver { | |
private var originalSoftInputMode: Int? = null | |
init { | |
fragment.lifecycle.addObserver(this) | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE) | |
fun replaceSoftInput() { | |
originalSoftInputMode = activity.window?.attributes?.softInputMode | |
activity.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) | |
fun restoreSoftInput() { | |
originalSoftInputMode?.let { activity.window?.setSoftInputMode(it) } | |
fragment.lifecycle.removeObserver(this) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment