Android Question Calling native java code

Yknot OCS

New Member
Licensed User
How would I define and call the following native java code (trying to "simulate" back button press WITHOUT calling Activity.Finish()

Public void simulateBackClick() {
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
}

Or if there is a better way of doing this I would be grateful!
 

JordiCP

Expert
Licensed User
Longtime User
You can try this example. Needs JavaObject library.
To simulate a the real back key click, uncomment last line :)

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub  Activity_Click
   Dim J As JavaObject
   J.InitializeContext
   J.RunMethod("simulateBackClick",Null)
End Sub

#if JAVA

import android.view.KeyEvent ;

public void simulateBackClick() {
   BA.Log("Hello!");
   this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
   
   //If you add this line, it will be a click and Activity_Pause will be called
   //this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}
#end if
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You can access the code using JavaObject :
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Resume

    Dim KE As JavaObject
    KE.InitializeNewInstance("android.view.KeyEvent",Array(0,4))
    Dim ActJO As JavaObject
    ActJO.InitializeContext
    ActJO.RunMethod("dispatchKeyEvent",Array(KE))
  
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Public Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    Log(KeyCode)
    'Don't exit of the back key is pressed
    If KeyCode = 4 Then Return True
End Sub

0 is the value of the constant ACTION_DOWN and 4 is the keycode for the Back Key
 
Upvote 0

Yknot OCS

New Member
Licensed User
Thank you guys for the quick reply - the 1st solution did not work as I need to call this code inside a class module, the 2nd solution works 100% - thank you
stevel05 for the solution!!!

Now for the "why do I need this part". This was part of an experiment (which by the way works 100% now) for an app where all the activities share the same top & bottom toolbar with a sliding menu.

Obviously, normally you would have to provide an event for each button on both toolbars AND sliding menu for each activity - needless to say a LOT of code duplication...

what I did was to create a class which will automatically "hook" to events from a GestureDetector for each button. This means I only have to write the code once for the functionality for each button (facebook link, twitter feed etc.).

The only issue was that I could not find a proper way to do "Activity.Finish" for the "back" button without the app crashing.
 
Upvote 0
Top