Logging out after successful Google+ sign in

Viewed 2066

This is a long question and might be confusing, please bear with me. So i successfully created a Google+ sign in for my Android app following the instructions on the developers.google.com website and it successfully signs a user in.Here is the code of my main Activity.

//..imports
@SuppressWarnings("unused")

public class MainActivity extends Activity implements
    ConnectionCallbacks, OnConnectionFailedListener, View.OnClickListener  {

private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
private static final String TAG = "MainActivity";

private ProgressDialog mConnectionProgressDialog;
private static PlusClient mPlusClient;
private ConnectionResult mConnectionResult;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mPlusClient = new PlusClient.Builder(this, this, this)
            .setActions("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity")
            .setScopes(Scopes.PLUS_LOGIN)  // recommended login scope for social features
                    // .setScopes("profile")       // alternative basic login scope
            .build();
    // Progress bar to be displayed if the connection failure is not resolved.
    mConnectionProgressDialog = new ProgressDialog(this);
    mConnectionProgressDialog.setMessage("Signing in...");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    findViewById(R.id.sign_in_button).setOnClickListener(this);
     return true;
}

protected void onStart() {
    super.onStart();
    mPlusClient.connect();
}

protected void onStop() {
    super.onStop();
    mPlusClient.disconnect();
}
public PlusClient getMplusClient() {
    return mPlusClient;
}
@Override
public void onConnectionFailed(ConnectionResult result) {
    if (mConnectionProgressDialog.isShowing()) {
        // The user clicked the sign-in button already. Start to resolve
        // connection errors. Wait until onConnected() to dismiss the
        // connection dialog.
        if (result.hasResolution()) {
            try {
                result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                mPlusClient.connect();
            }
        }
    }

    // Save the intent so that we can start an activity when the user clicks
    // the sign-in button.
    mConnectionResult = result;
}




protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
        mConnectionResult = null;
        mPlusClient.connect();
    }
}

public void onConnected(Bundle connectionHint) {
    String accountName = mPlusClient.getAccountName();
    Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();
    startActivity(new Intent(this, TestActivity.class));
}



public void onDisconnected() {
    Log.d(TAG, "disconnected");
}
public void startTestActivity() {
    startActivity(new Intent(this, TestActivity.class));
}

@Override
public void onClick(View view) {

    if (view.getId() == R.id.sign_in_button && !mPlusClient.isConnected()) {
        if (mConnectionResult == null) {
            mConnectionProgressDialog.show();
        } else {
            try {
                mConnectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
            } catch (SendIntentException e) {
                // Try connecting again.
                mConnectionResult = null;
                mPlusClient.connect();
            } } } }

}//end of class

`

Now i added a new activity to my project called TestActivity where i have the code to sign a user out of the app.And this TestActivity is started in the MainActivity from the block

 public void onConnected(Bundle connectionHint) {
    String accountName = mPlusClient.getAccountName();
    Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();
    startActivity(new Intent(this, TestActivity.class));
}

This works fine and the TestActivity is started when a user is connected to the app, and when i run the app again, this is the activity is see(since the user is still signed in).

Now, the problem is the logout button in the TestActivity class does not do anything.Google's Sign Out method is

public void onClick(View view) {
if (view.getId() == R.id.sign_out_button) {
    if (mPlusClient.isConnected()) {
        mPlusClient.clearDefaultAccount();
        mPlusClient.disconnect();
        mPlusClient.connect();
    }
}
}

Since mPlusClient is in the MainActivity and is declared as private i added the getMPlusCLient() method you see in the MainActivity (I think this is where my problem starts) which returns mPlusClient, a PlusClient object.Now, i ended up with this as my TestActivity

public class TestActivity extends Activity implements View.OnClickListener{

private Button sign_out_button;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.test, menu);

    sign_out_button = (Button)findViewById(R.id.signOutButton);

    sign_out_button.setOnClickListener(this);

    return true;
}
public void signOut() {
    MainActivity signOutObject = new MainActivity();
    if (signOutObject.getMplusClient().isConnected()) {
        signOutObject.getMplusClient().clearDefaultAccount();
        signOutObject.getMplusClient().disconnect();
        signOutObject.getMplusClient().connect();
    }


}

@Override
public void onClick(View view) {
    switch(view.getId()) {

    case R.id.signOutButton:
        signOut();
        break;
    }

}

    }

The problem like i stated earlier is the logout button in my TestActivity class does not do anything and no error message is shown in logcat.Where did i go wrong?

0 Answers
Related