Java Question Can we call finishActivity() for an activity Started for Result?

NFOBoy

Active Member
Licensed User
Longtime User
Erel,

in the button clicker example, there is piece of code that allows the Activity that calls an Activity for Result to do this:

B4X:
   // Forcibly dismiss the waiting room UI (this is useful, for example, if we realize the
    // game needs to start because someone else is starting to play).
    void dismissWaitingRoom() {
        mWaitRoomDismissedFromCode = true;
        finishActivity(RC_WAITING_ROOM);
    }

Is this possible with b4a? If not, trying to see how this would happen (game starting when people are in the waiting room, and how to avoid it)

Ross
 

NFOBoy

Active Member
Licensed User
Longtime User
So this means that the Activity that called the StartActivityForResult, will be active and running? (or paused and running?)

Specifically, the specific example is the OnRealTimeMessageRecieved Listener. While someone is in the WaitingRoom activity, if a message is received saying, "hey, start the game" , then the finishActivity is run which kills the WaitingRoom and returns back to the Game Activity.


So in the RealTimeListener, I can call something that will be activated even when in the WaitingRoom?

Hrmmm.. (totally missing how this works with Activities in the background...)
 

NFOBoy

Active Member
Licensed User
Longtime User
That's what I'm trying to understand... from the example ButtonClicker

B4X:
    // Called when we receive a real-time message from the network.
    // Messages in our game are made up of 2 bytes: the first one is 'F' or 'U'
    // indicating
    // whether it's a final or interim score. The second byte is the score.
    // There is also the
    // 'S' message, which indicates that the game should start.
    @Override
    public void onRealTimeMessageReceived(RealTimeMessage rtm) {
        byte[] buf = rtm.getMessageData();
        String sender = rtm.getSenderParticipantId();
        Log.d(TAG, "Message received: " + (char) buf[0] + "/" + (int) buf[1]);

        if (buf[0] == 'F' || buf[0] == 'U') {
            // score update.
            int existingScore = mParticipantScore.containsKey(sender) ?
                    mParticipantScore.get(sender) : 0;
            int thisScore = (int) buf[1];
            if (thisScore > existingScore) {
                // this check is necessary because packets may arrive out of
                // order, so we
                // should only ever consider the highest score we received, as
                // we know in our
                // game there is no way to lose points. If there was a way to
                // lose points,
                // we'd have to add a "serial number" to the packet.
                mParticipantScore.put(sender, thisScore);
            }

            // update the scores on the screen
            updatePeerScoresDisplay();

            // if it's a final score, mark this participant as having finished
            // the game
            if ((char) buf[0] == 'F') {
                mFinishedParticipants.add(rtm.getSenderParticipantId());
            }
        } else if (buf[0] == 'S') {
            // someone else started to play -- so dismiss the waiting room and
            // get right to it!
            Log.d(TAG, "Starting game because we got a start message.");
            dismissWaitingRoom();
            startGame(true);
        }
    }

and then the called code

B4X:
 // Forcibly dismiss the waiting room UI (this is useful, for example, if we realize the
    // game needs to start because someone else is starting to play).
    void dismissWaitingRoom() {
        mWaitRoomDismissedFromCode = true;
        finishActivity(RC_WAITING_ROOM);
    }


And this is how the Activity is declared

B4X:
public class MainActivity extends BaseGameActivity
        implements View.OnClickListener, RealTimeMessageReceivedListener,
        RoomStatusUpdateListener, RoomUpdateListener, OnInvitationReceivedListener {

I haven't been able to test this out, as I don't have three devices convenient to see about setting up a three way game, where two people decide to start without the third.. (or fourth)
 

NFOBoy

Active Member
Licensed User
Longtime User
Erel found this:


When one player clicks the Start Playing option, the waiting room UI is not automatically dismissed for the other players in the game. To dismiss the waiting room for the other players, send a reliable real-time message to the other players to indicate that the game is starting early. When your app receives the message, it should dismiss the waiting room UI:

boolean mWaitingRoomFinishedFromCode = false;

// if "start game" message is received:
mWaitingRoomFinishedFromCode = true;
finishActivity(RC_WAITING_ROOM);

The mWaitingRoomFinishedFromCode flag is necessary because dismissing the waiting room as shown above causes a result code of Activity.RESULT_CANCELED to be returned. You must differentiate this case from the case where the player has dismissed the waiting room using the back button

So i am assuming we can use it (as I don't see any special thing about how the activity was declared, as in my above post) I just don't know what parameter should be passed to finishActivity("what goes here");
 

NFOBoy

Active Member
Licensed User
Longtime User
Erel, this is how to call an ActivityForResult with ion:

B4X:
public void showWaitingRoom(RoomWrapper rw, GameClientWrapper GCW) {

        // minimum number of players required for our game
        final int MIN_PLAYERS = 2;
        Intent i = GCW.me.getRealTimeWaitingRoomIntent(rw.getRoom(), MIN_PLAYERS);
        ion = new IOnActivityResult() {
           @Override
           public void ResultArrived(int resultCode, Intent data) {
              debugLog("Waiting Room Done");
               mBA.raiseEvent(this, eventName +"_onwaitingroomresponse", resultCode, data);
           }
       };
       mBA.startActivityForResult(ion, i);

    }

and this is how it is done in GoogleGameServices


B4X:
  void showWaitingRoom(Room room) {
        mWaitRoomDismissedFromCode = false;

        // minimum number of players required for our game
        final int MIN_PLAYERS = 2;
        Intent i = getGamesClient().getRealTimeWaitingRoomIntent(room, MIN_PLAYERS);

        // show waiting room UI
        startActivityForResult(i, RC_WAITING_ROOM);
    }


The code:

B4X:
void dismissWaitingRoom() {
        mWaitRoomDismissedFromCode = true;
        finishActivity(RC_WAITING_ROOM);
    }

is what calls finishActivity

1). finishActivity requires an int argument... don't see that when calling ActivityForResult using the ion method.

2) finishActivity is an Activity method, and I don't know how to implement that in my classWrappers....

My feeling would be we would have to use something like

mBA.finishActivity(ion), or something like that?

Ross
 
Top