OnTouchEventListener stop working after adding inner Fragments on Activity
See original GitHub issueHi, I’m trying to use OnTouchEventListener like this:
class PingDispatcherLifecycleCallback : Application.ActivityLifecycleCallbacks {
private val pingDispatcher = OnTouchEventListener { motionEvent ->
if (motionEvent.action == KeyEvent.ACTION_DOWN) {
Log.d("ping", "send")
}
}
override fun onActivityResumed(activity: Activity) {
activity.window.touchEventInterceptors += pingDispatcher
}
override fun onActivityPaused(activity: Activity) {
activity.window.touchEventInterceptors -= pingDispatcher
}
...
}
if put Fragment into Activity and add child Fragment into root Fragment It stop to receive onTouch events
it become works after adding this callback into PingDispatcherLifecycleCallback in onActivityResumed callback
class PingDispatcherLifecycleCallback : Application.ActivityLifecycleCallbacks {
...
private val fragmentOnAttachListener = object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
window.touchEventInterceptors -= pingDispatcher
window.touchEventInterceptors += pingDispatcher
}
}
override fun onActivityResumed(activity: Activity) {
supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentOnAttachListener, true)
}
...
}
is it bug or legal behaviour of Android?
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
setOnTouchListener not working for android Fragment
I want to get the Touch event when the user touches any of the fragments. Fragment Code public MobileBankingFragment() { // Required empty ......
Read more >Fragment | Android Developers
A Fragment is closely tied to the Activity it is in, and can not be used ... is stopped, no fragments inside of...
Read more >Creating and Using Fragments | CodePath Android Cliffnotes
Hiding and showing relevant fragments using the fragment manager. ... onDetach() is called when fragment is no longer connected to the activity.
Read more >Building dynamic user interfaces in Android with fragments
As it is possible to dynamically add and remove fragments from an activity. The usage of fragments allows to design very flexible user...
Read more >Android Fragments Tutorial: An Introduction with Kotlin
The following lifecycle events come into play when you add a fragment: onAttach : The fragment attaches to its host activity. onCreate :...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Closing, partially fixed by https://github.com/square/curtains/pull/18 (though really the fix is in appcompat)
I do expect the callback to be replaced, but the key difference / expectation is that I expect it to be chained, ie I expect
ToolbarActionBar$ToolbarCallbackWrapperto wrapAppCompatDelegateImpl.AppCompatWindowCallbackand thenAppCompatDelegateImpl.AppCompatWindowCallbackto wrapcurtains.internal.WindowCallbackWrapperHowever, as I was saying, this won’t work with the current release. The next release, after #18 is merged, will make that change work.