Java Question Handle return value with RaisEvent

Discussion in 'Libraries developers questions' started by XverhelstX, Jul 12, 2013.

  1. XverhelstX

    XverhelstX Well-Known Member Licensed User

    Hi

    How is it possible to return an event from a raisevent from an external activity.

    For example in Basic4Android we have this:

    Code:
    Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
       If Keycode = KeyCodes.KEYCODE_BACK Then
          
    Return False
       
    Else
          
    Return False
       
    End If   
    End Sub
    In java:

    Code:
    @Override
        
    public boolean onKeyDown(int keyCode, KeyEvent event) {
          
    return false;
          
       
    }
    How is it possible to let the user choose to return false or true when an event is raised to a b4a sub

    Regards,
    Tomas
     
    Last edited: Jul 12, 2013
  2. Informatix

    Informatix Expert Licensed User

    raiseEvent and raiseEvent2 return an object by default. If you want to return a boolean, you just have to typecast the returned object.
    Example:
    Code:
    boolean YesNo = (boolean) m_Grid.m_BA.raiseEvent2(this, false, m_Grid.m_EventPrefix + "_question"true, new Object[] { this.X, this.Y });
     
  3. XverhelstX

    XverhelstX Well-Known Member Licensed User

    Great and thanks!
     
  4. daemon

    daemon Active Member Licensed User

    What will happen if the event handler sub does not exist?
     
  5. Informatix

    Informatix Expert Licensed User

    With RaiseEvent, nothing happens. With RaiseEvent2, it depends on the value of the second parameter.
     
    daemon and Erel like this.
  6. Erel

    Erel Administrator Staff Member Licensed User

    Small comment about the original question.

    This code is safer:
    Code:
    Boolean yesNoOrNull = (Boolean)ba.raiseEvent(...);
    boolean yesNo = yesNoOrNull != 
    null && yesNoOrNull == true;
    There are cases where raiseEvent or raiseEvent2 will return null. For example if the activity is currently not active.
     
    daemon likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice