Using `setContent` in an Activity seems to kill th...
# compose
c
Using
setContent
in an Activity seems to kill the ability to use
Activity.onTouchEvent()
Is this a bug? 🧵
Here's what I have with xml based layouts
Copy code
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.myButton).setOnClickListener {
            Toast.makeText(this, "XML BUTTON CLICKED", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_UP) {
            Toast.makeText(this, "ACTIVITY CLICKED", Toast.LENGTH_SHORT).show()
            return true
        }
        return false
    }
}
The xml based layout has a button in the top left corner. When I click the button, I see the XML BUTTON CLICKED toast... and anywhere I click in the Activity I see ACTIVITY CLICKED
This is with compose
Copy code
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val context = LocalContext.current
            Column(Modifier.fillMaxSize()) {
                Button({
                    Toast.makeText(context, "COMPOSE BUTTON CLICKED", Toast.LENGTH_SHORT)
                        .show()
                }) {
                    Text("Button")
                }
            }
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_UP) {
            Toast.makeText(this, "ACTIVITY CLICKED", Toast.LENGTH_SHORT).show()
            return true
        }
        return false
    }
}
Which does not work. The activity clicked never shows. But the COMPOSE CLICKED does. Ideas?
a
The doc of
onTouchEvent
says
Called when a touch screen event was not handled by any of the views under it.
And ComposeView is intercepting all the touch events.
c
Is that something I can opt out of? It kinda kills my Kiosk software 😭
a
No.
😭 1
If you really need that you can try
dispatchTouchEvent
.
c
Hm. dispathTouchEvent works sort-of. When I click on the compose button, now I get two events. The button AND the activity touch event. Am I missing something, or am I basically stuck with triggering both events in that scenario?
a
If what you want is to detect unhandled clicks, I don't think it's possible at activity level.
c
darn. Okay. I guess I'm going to have to rethink this. I've been using fragments in a single activity app, and I have always gone this route and it worked well. Now adding composables into the mix kinda "ruins" it. Will keep trying a few things. I did file a bug for this, so I guess I'll see if there's anyway to get around it
541 Views