2

I have an activity A, where I launch another activity through intent. But my objective is to pass the keys down to activity1, instead of handling at activity2. Returning false from onkeydown or onkeyup is no use. How can I achieve this?

Class activity1: Activity(){

Override oncreate(){
    // set view
    // launch activity2
    launchactivy2()
}

fun launchactivy2(){
    val playIntent = Intent("android.intent.action.VIEW")
    playIntent.putExtra("position", "top")
    playIntent.component = ComponentName(
            "com.myapp.package”,
        "com.myapp.package.activity2”
    )
    context.startActivity(playIntent)
    }

fun onKeyDown(){
    // Handle keys here
} 
}

my activity2 looks like below, where I'm not handling keys, but retuning false.

Class activity2: Activity(){
Override oncreate(){
    // set view
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
    super.onKeyDown(keyCode, event)
    return false
}
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
    super.onKeyUp(keyCode, event)
    return false
}
}

Tried only returning false, instead of super call from onKey.. methods and of no use. Is it possible to achieve this?

NoAIUser
  • 3,966
  • 6
  • 34
  • 52
  • Why can't Activity1 override the same methods overridden on `activity2`? – cutiko Jun 25 '21 at 14:28
  • @cutiko Have no control over activity2, except for keyHandling – NoAIUser Jun 25 '21 at 14:29
  • I'm asking for `activity1 `, and how can you have no control if you are overriding methods on `activity2`? You are literally able to write code there, how do you not have control? – cutiko Jun 25 '21 at 14:31
  • Yeah why not just call method `onKeyDown()` for `Activity1` in `Activity2`? Or better make `Activity2` extends `Activity1`. – Darkman Jun 25 '21 at 14:39
  • They're from different applications, not owned by me. – NoAIUser Jun 25 '21 at 14:45
  • Ahh then if its from different applications then you can't do this. Only thing you can do is create an event through intents in the one app and have the event listened to in the other. Its not a guaranteed delivery but that is about all you can do. https://developer.android.com/training/basics/intents – JPM Jun 25 '21 at 19:54

1 Answers1

0

Is it possible to achieve this?

No. Each activity is its own window; key events go to a window.

The simplest solution is to have a single activity, rather than two.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • can you check my post as well https://stackoverflow.com/questions/68113141/exception-in-thread-main-coroutine2-java-lang-nullpointerexception-in-viewmo – Edgar Jun 25 '21 at 14:33