I am integration Google Plus in android application. I have done all sign in process in Login Screen and after the success scenario I just finish that activity and moved to next activity which is the HomeActivty. I have to sign out out the Google Plus from this HomeActivity. Issue is that I have declared all GoogleApiClient in Login Activity. I got a code while searching for sign out that requires GoogleApiClient object. Error is that No activity present because I have already finish that activity after sign in. My question is about Can you please suggest me to know that if a user is not logged out I have to check and make him logged out from an another activty ?
My Login Activity code
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_gplus:
GlobalPreferManager.setString(Keys.SIGN_IN_METHOD, "google_plus");
if (!new ConnectionInfo(getActivity()).isConnectingToInternet()) {
CommonUtils.showToast(getActivity(), "No Internet Connection");
} else {
signIn();
}
break;
}
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
GlobalPreferManager.setBoolean(Keys.IS_LOGGED_IN, true);
GlobalPreferManager.setString(Keys.SIGN_IN_METHOD, "google_plus");
startActivity(new Intent(getActivity(), LocationActivity.class));
getActivity().finish();
} catch (ApiException e) {
CommonUtils.showToast(getActivity(), "Failed to connect!");
}
}
My Home Activity Code
@Override
public void onClick(View view) {
Auth.GoogleSignInApi.signOut(LoginFragment.mGoogleApiClient);
}
All you have to do is write all of your google integration code in different class GooglePlusLogin
(you can name whatever you want) and by creating object
of that class in your CallingActivity
to make google login, and you can call signOut()
method by making the signOut() accessable.
public class GooglePlusLogin implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
private GoogleApiClient mGoogleApiClient;
FragmentActivity context;
public static final int RESULT_OK = -1;
public static final int RC_SIGN_IN = 9001;
private OnClientConnectedListener listener;
private int type;
public GooglePlusLogin(FragmentActivity context, OnClientConnectedListener listener) {
this.context = context;
this.listener = listener;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addScope(new Scope(Scopes.PROFILE))
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_SIGN_IN && mGoogleApiClient.isConnected()) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
} else {
signIn(type);
}
}
public void signIn(int type) {
this.type = type;
revokeAccess();
}
private void revokeAccess() {
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
((Activity) context).startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
String imageUrl = "";
String email = "";
String fullname = "";
if (acct.getPhotoUrl() != null) {
imageUrl = acct.getPhotoUrl().toString();
}
if (acct.getEmail() != null) {
email = acct.getEmail();
}
if (acct.getDisplayName() != null) {
fullname = acct.getDisplayName();
}
acct.getIdToken();
listener.onGoogleProfileFetchComplete(fullname, acct.getId(), email, imageUrl, type);
signOut();
} else {
listener.onClientFailed(type);
signOut();
}
public void onStart() {
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
public void onStop() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed:" + connectionResult);
((OnClientConnectedListener) context).onClientFailed(type);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
// implement this interface in your `CallingActivity`
public interface OnClientConnectedListener {
void onGoogleProfileFetchComplete(String fullname, String acctId, String email, String imageUrl, int forType);
void onClientFailed(int forType);
}
}
// Implementation in LoginActivity
public class LoginActivity extends Activity implements GooglePlusLogin.OnClientConnectedListener {
public GooglePlusLogin plusLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initViews();
plusLogin = new GooglePlusLogin(this, this);
}
// this is becuase I haven't used enableAutoManage() while connecting to googleApiClient in GooglePLusLogin
@Override
protected void onStart() {
super.onStart();
if (plusLogin != null) {
plusLogin.onStart();
}
}
@Override
protected void onStop() {
super.onStop();
if (plusLogin != null) {
plusLogin.onStop();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GooglePlusLogin.RC_SIGN_IN) {
plusLogin.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onGoogleProfileFetchComplete(String fullname, String acctId, String email, String imageUrl, int forType) {
// here you will get all the details of user
plusLogin.signOut();
}
@Override
public void onClientFailed(int forType) {
plusLogin.signOut();
}
}