Android Question How to code wait for result of user's pressed the key

Theera

Expert
Licensed User
Longtime User
Assume I've the code ,I need to use Wait For (UserKeyPressed) Complete (Unused As Boolean),I don't know how to code.

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode=KeyCodes.KEYCODE_BACK Then
        #If B4A
            Dim MC As MenuClose
            MC.Initialize(Colors.ARGB(200,150,150,150),Colors.Blue)
        #End If
        Return True
    End If
     
    Return False
    

'    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub
 

Theera

Expert
Licensed User
Longtime User
In the Main module ,how to code stead of Activity.Finish order to call SD_MenuClose?
 

Attachments

  • $APPNAME$.zip
    73.1 KB · Views: 23
Upvote 0

emexes

Expert
Licensed User
Does the B4A app see KEYCODE_BACK?

I thought that key (and the two other system buttons) were handled by Android.

Having said that, I'll give it a try here whilst I've got the phone connected.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Does the B4A app see KEYCODE_BACK?

I thought that key (and the two other system buttons) were handled by Android.

Having said that, I'll give it a try here whilst I've got the phone connected.
We have to code callsub, or not? I 'm not good at coding. I have tried.
CallSub (me, Activitivity2)
 
Upvote 0

emexes

Expert
Licensed User
Dare I ask: what are we actually trying to do?

Like, doesn't pressing the back button take you back to the previous activity anyway?

Btw sorry I got distracted, now I'm *really* going to give it a try *now*.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Dare I ask: what are we actually trying to do?

Like, doesn't pressing the back button take you back to the previous activity anyway?

Btw sorry I got distracted, now I'm *really* going to give it a try *now*.
You're asking what the purpose of running Sd_Menuclose is, if yes, I'll be honest, I love this library. It may seem a bit chaotic, but it makes the app look different from the app.
 
Upvote 0

emexes

Expert
Licensed User
Well, I've gotten as far as confirming that the Activity_Keypress event handler Sub in the Main module is indeed receiving the Back button press, and also the Volume Up and Down button presses, and that if you Return True to indicate that you've handled the button press, then the usual system response doesn't happen.

Perhaps add a Log(KeyCode) line to your Activity+Keypress event handler, make sure that you're actually getting the keystrokes so that you can redirect the Back button handling to StarDust's menu close routine.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Try with this for the call
B4X:
Wait For BackWasPressed    'BackWasPressed can be anything else you want with a meaning for you
Callsub(Me, Activity2)
and this for the KeyPress
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode=KeyCodes.KEYCODE_BACK Then
        #If B4A
            Dim MC As MenuClose
            MC.Initialize(Colors.ARGB(200,150,150,150),Colors.Blue)
            CallSubdelayed(Me, "BackWasPressed")    'Here you need to use what you wrote in the Wait For
        #End If
        Return True
    End If
   
    Return False
   

'    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Try with this for the call
B4X:
Wait For BackWasPressed    'BackWasPressed can be anything else you want with a meaning for you
Callsub(Me, Activity2)
and this for the KeyPress
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode=KeyCodes.KEYCODE_BACK Then
        #If B4A
            Dim MC As MenuClose
            MC.Initialize(Colors.ARGB(200,150,150,150),Colors.Blue)
            CallSubdelayed(Me, "BackWasPressed")    'Here you need to use what you wrote in the Wait For
        #End If
        Return True
    End If
  
    Return False
  

'    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub
I'm sorry to reply lately, because Yesterday I just went to court about my case, so I answered today. I've tested the recommended code, but it still seems like there's a problem.
In Activity_Create module
B4X:
StartActivity(Activity2)
'Activity.Finish
Wait For BackWasPressed
CallSub(Me, Activitivity2)
After Launch the app, the app never shows activity2 and then closes itself.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Try with this for the call
B4X:
Wait For BackWasPressed    'BackWasPressed can be anything else you want with a meaning for you
Callsub(Me, Activity2)
and this for the KeyPress
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode=KeyCodes.KEYCODE_BACK Then
        #If B4A
            Dim MC As MenuClose
            MC.Initialize(Colors.ARGB(200,150,150,150),Colors.Blue)
            CallSubdelayed(Me, "BackWasPressed")    'Here you need to use what you wrote in the Wait For
        #End If
        Return True
    End If
 
    Return False
 

'    Return B4XPages.Delegate.Activity_KeyPress(KeyCode)
End Sub
I'm sorry to reply lately, because Yesterday I just went to court about my case, so I answered today. I've tested the recommended code, but it still seems like there's a problem.
In Activity_Create module
B4X:
StartActivity(Activity2)
'Activity.Finish
Wait For BackWasPressed
CallSub(Me, Activity2)
After Launch the app, the app never shows activity2 and then closes itself.
 
Upvote 0
Top