How to pass GoogleApiClient connections to another activity - android

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...)

Related

Nearby Connections advertise with application context

I'm using Nearby Connections framework for Quiz Game application. Also, I'm using MVVM design pattern along with live data and room. When I call Nearby.getConnectionsClient() in activity, I can pass this as argument or applicationContext as argument and everything works. But I don't want handle networking in activity, I want do this for example in view model (or maybe some singleton). But when I use AndroidViewModel(application) and as argument pass application.applicationContext, which is same application context like in activity, it doesn't work. When I'm trying to advertise, I get com.google.android.gms.common.api.ApiException: 13: ERROR. I have no idea what is difference and why this is not working. Plus when I am using activity context and rotate a phone, it got destroyed. So I need start advertising again?
I'm working on quiz app and I want to create lobby activity, where you can find nearby HOSTS and join. Then I want to have access to this connection in next activities, where game is playing. So also I don't have idea how to work with this connection in multiple activities.
Thanks for help.
Using the application context definitely works. Try using the application object itself instead of Application.applicationContext

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

Android design using Google Plus: How do I GoogleApiClient.connect again?

So, I have a bunch of activities. The flow is as follows:
Main Activity: I connect to Google Plus account here, then call Show Logs, to get data from Google account.
I also have About and Setting activities.
Now if I go to About then come back to Logs, Android throws error that GoogleApiClient is not connected. So should I again call the MainActivity for connecting else should I move all of the connection code to Common or Base Activity.
I am sorry if I am not clear enough.
If the GoogleApiClient is instantiated in the Main activity, once you leave that activity it will disconnect.
For your purposes you can try having multiple instances of the GoogleApiClient across your activities (might be inefficient) but Google's interface will not require the user to sign in multiple times for each activity. Each instance will access the same state.
You can check this post for more info
Access google plus client from multiple activities
Or have try to implement the connection in an AsyncTask that will keep running in the background.
Have a look at this link
http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

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.

How can I test an Activity that depends on another one being created?

I'm new on Unit Testing.
I want to test an activity called Login. I'm extending ActivityInstrumentationTestCase2<Login> from the out-of-the-box api to develop the testing class.
The problem is that the Login Activity is started after another Activity,
which in turn, is started by a splash screen Activity.
The activity started in the middle starts some services and initialize some objects needed for the Login Activity I'm trying to test to work properly, so I need to have it created first.
How can I do that?
Thanks in advance.
ActivityInstrumentationTestCase2 provides functional testing of a single Activity. You need a different approach to test more than one Activity.
You could broadcast an Intent after everything you need has been initialized, then create a BraodcastRecevier to start the login process.
Alternatively, you could use message/handler communication to handle when a new activity is launched.

Categories

Resources