Java Question OnStart and OnStop issue

NFOBoy

Active Member
Licensed User
Longtime User
Erel, after reading as much as I could between your posts and StackExchange, etc... While wrapping the Game Services library I came across this:

B4X:
 /** Call this method from your Activity's onStart(). */
    public void onStart(BA ba, Activity act, Boolean AutoSignIn) {
        mActivity = act;
        mBA = ba;
        debugLog("onStart.");
        
        if(mExpectingActivityResult && AutoSignIn  && !lastTry) {
           connectCurrentClient();
        } else if (mExpectingActivityResult) {
            // this Activity is starting because the UI flow we launched to
            // resolve a connection problem has just returned. In this case,
            // we should NOT automatically reconnect the client, since
            // onActivityResult will handle that.
           Toast.makeText(getContext(), "mExpectingActivityResult", Toast.LENGTH_LONG).show();
            debugLog("onStart: won't connect because we're expecting activity result.");
            resolveConnectionResult();
        } else if (!AutoSignIn) {
            // The user specifically signed out, so don't attempt to sign in
            // automatically. If the user wants to sign in, they will click
            // the sign-in button, at which point we will try to sign in.
           Toast.makeText(getContext(), "No auto Sign in", Toast.LENGTH_LONG).show();
            debugLog("onStart: not signing in because user specifically signed out.");
        } else {
            // Attempt to connect the clients.
            debugLog("onStart: connecting clients.");
            Toast.makeText(getContext(), "starting Connections", Toast.LENGTH_LONG).show();
            startConnections();
        }
    }

    /** Call this method from your Activity's onStop(). */
    public void onStop() {
        debugLog("onStop: disconnecting clients.");

        // disconnect the clients -- this is very important (prevents resource
        // leaks!)
        killConnections(CLIENT_ALL);

        // no longer signed in
        mSignedIn = false;
        mSignInError = false;

        // destroy progress dialog -- we create it again when needed
        dismissDialog();
        mProgressDialog = null;

        // let go of the Activity reference
        mActivity = null;
        mBA = null;
    }

Based on the resource leaks part, I have made sure to mark this as an ActivityObject, and I run onStart in Activity_Resume, and onStop in Activity_Pause


However, this does create an issue that, as written, gives me an eternal loop when there is no connection available. Not terribly problematic, as I detected two tries in succession, and then killed any more attempts.

(because I call onStop, everything is reset, and then onStart tries to reconnect by calling another new ResultForConnection... onStop resets everything..... loop)

If I DON'T call onStop, then it works perfectly, but then I'm concerned by the potential resource leaks.


This also relates to Room Creation.. when I do all the stuff to build a Room, part of the process is to do an ActivityForResult that lets players choose opponents, respond to invitations, etc....

If I do it the OnStop, OnStart way... I was getting an error, because the time it takes to create the GameClient connection again, while small, is enough to cause errors when immediately setting up the Room Connection stuff.

If I don't do the onStop (and only call onStart in Activity_Create), it again works perfectly.

I have tried, and it works, to put a timer in on the listener to delay creating rooms, so can get around that without to much hassle, but it seems less than elegant.

Just wondering, do I need to enforce the onStop, onStart in Pause/Resume.. or is it ok to not call onStop?

Ross

P.S. hoping to release a version soon for people to test out with ability to create/control leaderboards and Achievements, and Room Creation (if I get the communication stuff down) later this week. I predict at least another couple of weeks for me to get all the nitty-gritty details down though.. if ever! ;)
 
Top