title: Google Sign-in "check first time login" and "Logout" issues in android application
body: <p>I am making an android application, when the user installs the application, he/she needs to sign-in with google account, after successful logged-in , he/she will be redirected to the dashboard page. If he/she closes application and opens it again and if they are already logged in then dashboard activity should be shown itself. Also I have a drawerLayout in my dashboard activity having a &quot;Sign-out&quot; button from which the user can log out and login activity is opened up again.</p>
  <ul>
  <li><p>I am using SharedPreference for checking First Time Login in android</p>
  <p>Below are the problems which I am facing</p>
  </li>
  <li><p>Application opens up the dashboard activity without asking for login.</p>
  </li>
  <li><p>When &quot;Sign-out&quot; is clicked from drawer layout in dashboard activity, for a second it goes to login activity but automatically log-in again. In short &quot;Sign-out&quot; is not working, it keeps me log-in.</p>
  </li>
  </ul>
  <p>Below is the screenshots when user opens up the app for first time. It doesn't ask me for login.
  <img src="https://i.stack.imgur.com/6maoP.png" alt="It doesn't ask for log-in" /></p>
  <p>But when i specifically clicks on signout, login activity is shown,
  <img src="https://i.stack.imgur.com/buD57.png" alt="enter image description here" /></p>
  <p>below I have shown the screenshot or the result, everytime I try to logout but it log-in me again. Any inputs will be appreciated.
  <img src="https://i.stack.imgur.com/Ip6Ry.png" alt="enter image description here" /></p>
  <p><strong>LoginActivity.java</strong></p>
  <pre><code>public class GooglePlayServicesActivity extends Activity implements OnClickListener, ConnectionCallbacks, OnConnectionFailedListener {
  
      private static final int RC_SIGN_IN = 0;
      private static final String TAG = &quot;GoogleLogin&quot;;
  
      // Google client to communicate with Google
      public GoogleApiClient mGoogleApiClient;
      private boolean mIntentInProgress;
      private boolean signedInUser;
      private ConnectionResult mConnectionResult;
      private SignInButton signinButton;
      private TextView username, emailLabel;
      private LinearLayout signInFrame;
      private RelativeLayout profileFrame;
      private View relativeLayout;
      private String personPhotoUrl;
      private String personID;
      private String personName;
      private String personEmail;
      private boolean hasLoggedIn;
      private String personGender;
      private String personDOB;
      private String personFullName;
      private String user_gender = &quot;&quot;;
      // DatabaseHandler dbHandler = new DatabaseHandler(this);
     public void checkLogIn(){
         Log.d(TAG,&quot;Checked Logged In called &quot;);
         SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);
         hasLoggedIn = settings.getBoolean(&quot;hasLoggedIn&quot;, false);
         Log.d(&quot;Value of HasLoggedin&quot;,&quot;:&quot;+String.valueOf(hasLoggedIn));
         if(hasLoggedIn){
             Intent i = new Intent(getApplicationContext(),ActivityFeedActivity.class);
             startActivityFeedActivity(i);
             GooglePlayServicesActivity.this.finish();
         }
     }
  
      private void startActivityFeedActivity(Intent i) {
          startActivity(i);
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          //checkLogIn();
          setContentView(R.layout.google_login);
          //initFacebookLogin();
          signinButton = (SignInButton) findViewById(R.id.signin);
          signinButton.setOnClickListener(this);
          relativeLayout = getLayoutInflater().inflate(R.layout.profile_view, null);
          username = (TextView) relativeLayout.findViewById(R.id.name);
          emailLabel = (TextView)  relativeLayout.findViewById(R.id.email);
  
          profileFrame = (RelativeLayout) relativeLayout.findViewById(R.id.profileView);
          signInFrame = (LinearLayout) findViewById(R.id.signinFrame);
  
          mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API, Plus.PlusOptions.builder().build()).addScope(Plus.SCOPE_PLUS_LOGIN).build();
      }
  
      protected void onStart() {
          super.onStart();
          mGoogleApiClient.connect();
      }
  
      protected void onStop() {
          super.onStop();
          if (mGoogleApiClient.isConnected()) {
              mGoogleApiClient.disconnect();
          }
      }
  
      private void resolveSignInError() {
          if (mConnectionResult.hasResolution()) {
              try {
                  mIntentInProgress = true;
                  mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
              } catch (SendIntentException e) {
                  mIntentInProgress = false;
                  mGoogleApiClient.connect();
              }
          }
      }
  
      @Override
      public void onConnectionFailed(ConnectionResult result) {
          if (!result.hasResolution()) {
              GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
              return;
          }
  
          if (!mIntentInProgress) {
              // store mConnectionResult
              mConnectionResult = result;
  
              if (signedInUser) {
                  resolveSignInError();
              }
          }
      }
  
      @Override
      protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
          switch (requestCode) {
              case RC_SIGN_IN:
                  if (responseCode == RESULT_OK) {
                      signedInUser = false;
  
                  }
                  mIntentInProgress = false;
                  if (!mGoogleApiClient.isConnecting()) {
                      mGoogleApiClient.connect();
                  }
                  break;
          }
      }
  
      @Override
      public void onConnected(Bundle arg0) {
          signedInUser = false;
          getProfileInformation();
          SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE); // 0 - for private mode
          SharedPreferences.Editor editor = settings.edit();
          editor.putBoolean(&quot;hasLoggedIn&quot;, true);
          editor.putString(&quot;personID&quot;, getPersonID());
          editor.putString(&quot;personName&quot;, getPersonFullName());
          editor.putString(&quot;personEmail&quot;,getPersonEmail());
          editor.putString(&quot;picURL&quot;,getPicUrl());
          editor.commit();
          Intent i = new Intent(this,ActivityFeedActivity.class);
          startActivityFeedActivity(i);
          GooglePlayServicesActivity.this.finish();
      }
  
  
      private void getProfileInformation() {
          try {
              if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                  Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
  
                  String personID = currentPerson.getId();
                  String personFullName = currentPerson.getDisplayName(); // Gives FullName
                  String personName = String.valueOf(currentPerson.getName()); //Gives First Name and Last Name in Json formay
                  String personGender = String.valueOf(currentPerson.getGender()); //Gives Gender in int 0:Male, 1:Female
                  String personDOB = currentPerson.getBirthday();
                  String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
                  String personPicUrl = currentPerson.getImage().getUrl();
  
                  setpersonID(currentPerson.getId());
                  setpersonFullName(currentPerson.getDisplayName());
                  setpersonName(currentPerson.getName());
                  setpersonEmail(email);
                  setpersonGender(personGender);
                  setPicUrl(currentPerson.getImage().getUrl());
                  setpersonDOB(currentPerson.getBirthday());
  
                  Log.d(TAG, &quot;Gender:&quot; + personGender + &quot; DOB:&quot; + personDOB + &quot; FirstName:&quot; + personName + &quot;  &quot;);
                  username.setText(personFullName);
                  emailLabel.setText(email);
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      @Override
      public void onConnectionSuspended(int cause) {
          mGoogleApiClient.connect();
         // updateProfile(false);
      }
  
      @Override
      public void onClick(View v) {
          switch (v.getId()) {
              case R.id.signin:
                  googlePlusLogin();
                  break;
          }
      }
  
      private void googlePlusLogin() {
          if (!mGoogleApiClient.isConnecting()) {
              signedInUser = true;
              resolveSignInError();
          }
      }
  
  }
  </code></pre>
  <p><strong>DashboardActivity.java</strong></p>
  <pre><code>private void initDrawerList(String[] values){
          this.drawerList = (ListView) findViewById(R.id.navdrawer);
          final DrawerLayout layout = this.drawerLayout;
          ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,
                  android.R.layout.simple_list_item_1, android.R.id.text1, values);
          this.drawerList.setAdapter(adapter);
          this.drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                                      int position, long id) {
                  switch (position) {
                      case 0:
                          layout.closeDrawer(Gravity.START);
                          break;
                      case 1:
                          layout.closeDrawer(Gravity.START);
                          break;
                      case 2:
                          layout.closeDrawer(Gravity.START);
                          break;
                      case 3:
                          googleLogout();
                          Log.d(TAG,&quot;Returned from function&quot;);
                          break;
                  }
              }
          });
      }
  
      private void googleLogout() {
          SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);
          SharedPreferences.Editor editor = settings.edit();
          editor.clear();
          editor.commit();
          if (mGoogleApiClient.isConnected()){
              Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
              mGoogleApiClient.disconnect();
              mGoogleApiClient.connect();
              Intent i = new Intent(this,GooglePlayServicesActivity.class);
              startActivity(i);
              ActivityFeedActivity.this.finish();
          }
          else {
              Log.d(TAG,&quot;entered else&quot;);
          }
  
          //updateProfile(false);
      }
  </code></pre>