question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OnTouchEventListener stop working after adding inner Fragments on Activity

See original GitHub issue

Hi, 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:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pyricaucommented, May 11, 2021

Closing, partially fixed by https://github.com/square/curtains/pull/18 (though really the fix is in appcompat)

0reactions
pyricaucommented, May 11, 2021

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$ToolbarCallbackWrapper to wrap AppCompatDelegateImpl.AppCompatWindowCallback and then AppCompatDelegateImpl.AppCompatWindowCallback to wrap curtains.internal.WindowCallbackWrapper

However, as I was saying, this won’t work with the current release. The next release, after #18 is merged, will make that change work.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found