Reputation: 107
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
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
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
Reputation: 86
What I think is you have 3 choices:
Upvotes: 5
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