Reputation: 853
In my android app, I have a GoogleApiClient mGoogleApiClient;
object. I want to pass it from the login.java class to MainActivity.java class.
I have tried to use Serializable and Parcelable.
Intent mainIntent = new Intent(this, MainActivity.class);
mainIntent.putExtra("googleapi", (Serializable) mGoogleApiClient);
startActivity(mainIntent);
I get this error java.lang.ClassCastException: com.google.android.gms.common.api.c cannot be cast to java.io.Serializable
.
Now if I try to pass a string, it works. I can understand that I can't cast a GoogleAPIClient class to Serializable.
How should I send this object to other class?
Upvotes: 3
Views: 3616
Reputation: 1568
ApplicationTest.Java
public class ApplicationClass extends Application
{
//instantiate object public static
public static GoogleApiClient mGoogleApiClient;
public Context mContext;
@Override
public void onCreate()
{
super.onCreate();
mContext = getApplicationContext();
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addConnectionCallbacks(mContext)
.addOnConnectionFailedListener(mContext)
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
// add other APIs and scopes here as needed
.build();
}
@Override
public void onTerminate()
{
super.onTerminate();
}
}
TestClass.Java
public class TestClass extends Activity
{
@Override
public void onCreate()
{
super.onCreate();
//this way you have can use object of mGoogleApiClient anywhere in the app.
ApplicationTest.mGoogleApiClient;
}
}
Upvotes: 2