Integrate Google Play Games + Google Maps extending BaseGamesActivity and FragmentActivity - android

I'm creating an application that uses Google maps to locate and display the current location of the user and some markers.
I would like to implement a check in part that will give points to the users and unlock achievements.
But I don't know how to extend both BaseGameActivity and FragmentActivity. A few hints will be appreciated.

Aha, a classic case of Activity superclass wars. Precisely with this problem in mind, we offer GameHelper as an alternative to BaseGameActivity, for games that for one reason or another cannot (or don't want to) change their Activity's base class. All you have to do is create a GameHelper object on your Activity's onCreate method and then let it know of important Activity lifecycle events, notably onStop, onStart and onActivityResult.
Take a look at the source code for BaseGameActivity as a reference. What you'll want to do is implement that same functionality in your Activity class so that you have a working GameHelper.
Particularly, you'll notice that BaseGameActivity is actually pretty dumb -- it just forwards events to GameHelper, which is really the brains behind it.

Related

Calling methods on activity events

I am writing a simple Android library. I have a couple of methods that I would like executed when certain activity events happen - like after activity got loaded or when the activity gets paused.
One way I could think of was to create a class that extends activity, and write my methods there, and then have "target" activities extend that class. This was all methods get called
But if the end user is extending some other activity already, this method won't work. Is there a better alternative?
If you need only provide support back to API 14 or higher, you might be able to make use of the application level activity lifecycle callbacks - see the Application.ActivityLifecycleCallbacks interface for details.
To make use of this you need to register an instance of this interface with an Application instance, using the registerActivityLifecycleCallbacks method. One way to get that would be to have the developer using your library initialise your library by passing their Application instance to it. This is what I do in an SDK that I maintain, and it seems to work nicely.

Persist Google Play Services login between activities

I thought this would be straightforward, but I've been going in circles trying to figure out how to keep a user logged in between Activities.
I have a "Main" and a "Details" Activity. A user logs into Google Play Services in the "Main" Activity and I want to submit achievements and leaderboard data in the "Details" Activity.
I'm inheriting from BaseGameActivity in both Activities and using:
mGoogleApiClient = getApiClient();
in "Details", however when I call isConnected it always returns false.
I even tried copying all the login/callback code from the "Main" Activity over, but it's still doesn't detect the user is logged in.
This post suggests not using BaseGameActivity and pass GameHelper using a singleton:
How to use BaseGameActivity.getApiClient() in multiple activities?
Not sure what the correct approach is.
The most simple way to do this is to have both activities create a separate instance of the api client. The state of the connection is shared between them internally, so you don't need worry about how to pass around the client and handle callbacks that may happen when an activity is not active, and the player will only log in on your main activity.
Extending your activity from BaseGameActivity really is not needed any longer (for an entertaining explanation watch: Death of BasegameActivity. What you do need to do is implement the two interfaces that handle initializing the GoogleAPIClient:
public class MainActivity extends Activity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
}
To implement these, refer to the samples and the doc: https://developers.google.com/games/services/android/init

How to organize Android App code?

First of all: I am rather new to Android App programming and I have a rather basic question:
Already with the sandbox app I am currently working on, the code in the Activity class get quite huge because all the callback methods / listeners (click listener, callbacks from GoogleApiClient) are in there (either by implementing the respective interface or by creating a private class). But I would rather put those into separate classes.
But the question that I ask myself is this: how would I then be able to access the class attributes of the activity class? Sure, I would then probably create setter/getter, but still I first need a reference to the Activity object. How would I get this?
Thanks and regards!
It's a really wide question, since the answer depends by your project and by your programming style. The first suggestion is: move what you can move in one or more fragment. All stuffs related to google play services can be nicely handled in a fragment for example. Listener and callback are UI related components, so they need a Context of an Activity to work, but you can split your UI (again) with Fragment and keep a piece of logic in a Fragment and another piece somewhere else. If you have some logic that runs in background, then you should consider using Service. I tend to have empty Activities, but this is not a rule.

How to pass GoogleApiClient connections to another activity

I want to connect to Google Play Games in my MainActivity then pass my mGoogleClientApi object to my GameActivity.
How do I go about it?
Plus! Every time I start the MainActivity is reconnects to GoogleApiClient.
Anything helps.
My suggestion would be, if you need to connect to google play games in the main activity
AND in the GameActivity, that you do exactly that thing. There is no need to hand over
a clientApi instance.
I assume that your main activity is kind of a login and afterwards you start the GameActivity?
Than use the api to connect in the first place, handle the login and start the GameActivity.
There you connect to the api again and handle the game relevant part.
Or you could use fragments and hand over the api client via a public method in your activity which
implements an interface to return the client.
Play Game Services are really made with Fragments in mind. It is almost mandatory to use Fragments instead of Activities, and to have a single Activity in your app (that you can made a subclass of GameActivity...)

How to use BaseGameActivity.getApiClient() in multiple activities?

In my game app I would like the user to sign in to Google Play Services during the starting activity so I had it subclass BaseGameActivity. Then a separate activity is started for the game, at the end of which I want to update a leaderboard using Google Play Services, which requires calling BaseGameActivity.getApiClient().
How should I use Google Play services from a different activity than the one that subclasses BaseGameActivity?
Two options I thought of were: pass a reference to the starting activity, or use a handler and send a message to the starting activity. But I don't know which would method would be better to use (or if a third way is better) and it seems like this might be a fairly common situation.
If you want to use GameHelper with multiple activities it is best to implement it without using BaseGameActivity and then pass your GameHelper instance between activities (or keep a static instance somewhere).
You can find instructions for using GameHelper directly on this page (see the Using GameHelper without BaseGameActivity heading).
Just make sure that you place the required GameHelper calls (especially onActivityResult) in all of your activities that make use of it. As for sign-on/sign-out you will need to determine for yourself in which activity(s) to place those, depending on the flow of your app.

Categories

Resources