0

I’m building an Android native application where most of the business logic resides in C++.

For example, UI events like Key Press or Drag-Drop arrive on the main thread. To process them, I forward these events into C++ through JNI. Sometimes, determining the action to take can be time-consuming, so I don’t want to block the main thread. The OS generally expects such work to be done on a background thread.

The challenge is:

If I move event handling to background threads, how do I ensure that events are still executed sequentially in the same order they were received?

What is the recommended approach to forward events to C++ while keeping UI responsive and event order preserved?

Here’s a simplified version of what I have now:

// Kotlin side (UI thread)
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
    // Forward event to C++ via JNI
    return NativeBridge.onKeyEvent(keyCode, event.action)
}
// C++ side
bool onKeyEvent(int keyCode, int action) {

    // This may take time...
    processEvent(keyCode, action);  // So, I want to make this call go async...
  
   return ? // Here, is where I am confused 
}

How should I structure this event dispatching (Java → JNI → C++ → background thread) so that:

  1. The UI thread isn’t blocked.
  2. Events are always processed in the same order they were received.

Some events (e.g., KeyPress on a TextField) have synchronous implications:

 If I return false, the OS also updates the text.
 If I return true, the OS does nothing, and I’d need to manually update the text.
 If I process these events asynchronously, I can’t immediately decide whether to return true or false. 
 This seems to force me into manual string management, which I’d like to avoid if possible. Is there a recommended way to handle such cases?

1 Answer 1

0

You can't do what you want. When onKeyDown returns, the OS will process the key event and display it. There is no way to prevent that. That means any function you wish to run, whether in Java, Kotlin, or C must execute before that function returns. And delaying the return by a significant amount of time will cause the app to freeze (and may even trip a watchdog timer in the OS). So you either need to speed up your processing, not do your processing, or find some way to do it in the background after the OS has made the key even display.

Now sometimes you are able to split off work to do in the background. For example, a searchbar will frequently start a network request on each character typed to update results. In this case it spins off another thread, performs the work on that thread, and updates the UI when done (and your app has to handle the cancelation case for old requests when a second character is typed). But that depends on there being work that doesn't actually need to happen immediately. If there is no such work, then what you want just isn't possible.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.