Skip to content

Instantly share code, notes, and snippets.

@featzima
Created April 4, 2019 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save featzima/2567135cbaff227a69453d7bf1e5269a to your computer and use it in GitHub Desktop.
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
/**
* 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