MainActivity
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks ,GoogleApiClient.OnConnectionFailedListener,View.OnClickListener {
private SignInButton signInButton;
private GoogleApiClient googleApiClient;
public static final String TAG="MainActivity";
private static final int RC_SIGN_IN=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleSignInOptions googleSignInOptions=new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
googleApiClient=new GoogleApiClient.Builder(this).enableAutoManage(this,this)
.addApi(Auth.GOOGLE_SIGN_IN_API,googleSignInOptions).build();
signInButton=(SignInButton)findViewById(R.id.mainactivitygooglesigninbutton);
signInButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.mainactivitygooglesigninbutton:
signIn();
break;
}
}
private void signIn() {
Intent intent=Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(intent,RC_SIGN_IN);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_SHORT).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==RC_SIGN_IN){
GoogleSignInResult result=Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
if(result.isSuccess()){
GoogleSignInAccount account=result.getSignInAccount();
Intent intent=new Intent(this,ContactsActivity.class);
startActivity(intent);
finish();
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (googleApiClient != null && googleApiClient.isConnected()) {
Intent intent=new Intent(this,ContactsActivity.class);
startActivity(intent);
finish();
}
}
@Override
public void onConnectionSuspended(int i) {
}
}
My MainActivity has the signin Option When i open my app and sign in it takes me to ContactsActivity.Now when i close my app and start again it shows me MainActivity again but i want to display ContactsActivity directly.Once user has successfully signed in and if he closes the app then i want to display ContactsActivity directly Please help.
Save login stage in a shared preference.
protected void onCreate(Bundle savedInstanceState) {
.
.
.
SharedPreferences sharedPreferences = getSharedPreferences("user_login_info",MODE_PRIVATE);
if(sharedPreferences.getBoolean(user_login,false)){
startActivity(new Intent(this,ContactsActivity.class));
finish();
}
}
private void handleSignInResult(GoogleSignInResult result) {
if(result.isSuccess()){
GoogleSignInAccount account=result.getSignInAccount();
Intent intent=new Intent(this,ContactsActivity.class);
SharedPreferences sharedPreferences = getSharedPreferences("user_login_info",MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putBoolean("user_login",true); //saving user info
editor.commit();
startActivity(intent);
finish();
}
}
when user sign out just clear the shared preference value
SharedPreferences sharedPreferences = getSharedPreferences("user_login_info",MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.clear();
editor.commit();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.