jl1990
jl1990

Reputation: 107

how to pass game center data between activities?

Well, I'm implementing the new game center in android and there is a concept that i don't understand.

I have a class which extends BaseGameActivity (https://developers.google.com/games/services/android/init), so it's the one who manages achievements, leaderboards, Google + sign in, etc. I need another class to be able to access game center data from that activity (unlock achievements, leaderboards, etc), but this class is not called from the extending BaseGameActivity class.

How can they communicate?

The structure is like this (meaning "->" that it starts other activity)

A (extends BaseGameActivity) -> B -> C (needs game center communication with A)

I have read the google's example (TypeANumber) but it uses a Listener to communicate, that's easy when the class you need to communicate with is the one which you are starting, but it's not as easy with my structure.

Any ideas?

Thanks

Upvotes: 5

Views: 863

Answers (4)

0909EM
0909EM

Reputation: 5027

Just ideas... I haven't looked at the code so treat this with a pince of salt..

Can you encapsulate inside a parcel able/serializable and then use set/getparceable in each activity that needs it? I'd recommend passing an interface down, but not sure how easy that'd be without looking at code

Alternatively can you wrap the game client logic in a service that you can interact with from each activity?

Upvotes: 0

Bruno Oliveira
Bruno Oliveira

Reputation: 5076

The most straightforward way to share a GamesClient between the multiple screens in a game is use Fragments, which I tried to illustrate in the TypeANumber sample.

That said, if you still want to have multiple Activities, each activity must manage its own GamesClient object, because a GamesClient is tied to a specific Activity. Therefore, if you're using the sample code, then each Activity would have to derive from BaseGameActivity, and each of them would get onSignInSucceeded(). In each of them, you can use getGamesClient() to obtain the GamesClient object for that Activity.

Upvotes: 1

groug
groug

Reputation: 86

What I think is you have 3 choices:

  1. Create a GamesClient for each Activity that needs it, but you have to call connect() for each activity, and it takes time. After the first connection, the other ones are much faster (a few hundred milliseconds)
  2. Create your own Application class and use it as a context for the GamesClient.Builder, and store your GamesClient object in your Application class. I've tested it, and it works. Now, I know it's discouraged to do that (because it breaks the Android philosophy) but it seems a lot of people prefer to create their own application class and store data into it instead of bundling data and passing them from one activity to another. Its main advantage is that you don't have to modify a lot of thing to make it work in your case (and in mine).
  3. Use Fragment classes instead of Activity classes and use a main Activity which owns the GamesClient object (in that case, your class C would be able to communicate with class A). Like in the example you're mentioning, there's only one Activity which inherits from Google example's BaseGameActivity (which contains the GamesClient instance), and the rest is just Fragment classes (each Fragment being a screen).

Upvotes: 5

Stochastically
Stochastically

Reputation: 7846

How about encapsulating all the data that needs to be shared into a singleton object, e.g.

class SharedData {
    public String aString;
    public double aDouble;
    // etc
    public static SharedData globalInstance = new SharedData();
}

and then all parts of your app can access via SharedData.globalInstance.

Upvotes: 0

Related Questions