3

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?

user2798227
  • 853
  • 1
  • 16
  • 31
  • possible duplicate of [How to correctly use Google Plus Sign In with multiple activities?](http://stackoverflow.com/questions/22368520/how-to-correctly-use-google-plus-sign-in-with-multiple-activities) – JP Ventura Jul 28 '15 at 16:40

2 Answers2

4

Try to declare mGoogleApiClient object in Application class.

2

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;
    }
}
Shujito
  • 3,083
  • 2
  • 17
  • 18
Jignesh Jain
  • 1,518
  • 12
  • 28
  • but in my login.java class, I am already extending ActionBarActivity class. I can't extend two classes. – user2798227 Mar 25 '15 at 04:11
  • Check above class you have to create a new application class please forget login class. – Jignesh Jain Mar 25 '15 at 04:20
  • So you mean I should create a ApplicationClass.java and extend Application the way you have done. Do I need to call this ApplicaitonClass from somewhere, or should I just create it this way and leave it? – user2798227 Mar 25 '15 at 04:22
  • Thanks, I have added these two files, but it still gives me the same error. But if I remove `mGoogleApiClient` object declaration from login file, it gives me `mGoogleApiClient` not found. And if I keep `private GoogleApiClient mGoogleApiClient;` declaration in login.java then it gives me the same error. – user2798227 Mar 25 '15 at 04:43
  • please check your declaration and sdk,check this link http://stackoverflow.com/questions/9093043/how-to-use-google-api-client-library – Jignesh Jain Mar 25 '15 at 05:01
  • I have checked the declaration and sdk. I have imported it correctly. – user2798227 Mar 25 '15 at 05:02