In my app I use single activity - MainActivity with multiple fragments. In Manifest file I set activity's windowSoftInputMode to adjustResize because I need that behaviour in majority of fragments where keyboard is shown.
<activity
android:name=".main.MainActivity"
android:configChanges="orientation|screenSize|layoutDirection"
android:exported="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
But a couple of pages by design don't need resizing behaviour when keyboard is shown. For that reason, I set softInputMode to SOFT_INPUT_ADJUST_NOTHING in onCreateView
and back SOFT_INPUT_ADJUST_RESIZE in onDestroyView
.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
return inflater.inflate(R.layout.fragment_b, container, false)
}
override fun onDestroyView() {
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
}
Everything works fine, but now I get a Deprecation warning for SOFT_INPUT_ADJUST_RESIZE with suggestion to use setDecorFitsSystemWindows
together with OnApplyWindowInsetsListener
. This solution is quite confusing to me as I don't understand why we need to setDecorFitsSystemWindows that I believe sets whether the content of the app can be drawn behind status and navigation bars and the end result is more lines of code compared to initial single line approach.
I have read the answer on this question SOFT_INPUT_ADJUST_RESIZE deprecated starting android 30 and the first answer shows how to use this new way, but it is not clear to me to which root view OnApplyWindowInsetsListener should be set and how to use this solution for SOFT_INPUT_ADJUST_RESIZE together with SOFT_INPUT_ADJUST_NOTHING because the later is not deprecated.
I would appreciate if anyone could help me to update my code to keep the same behaviour of the app as before but migrate to use new apis.