When the app is downloaded from Google Play, signing in on the LoginActivity does not launch the MainActivity. It works when deployed directly onto the device from Android Studio, though. I'm not sure what the problem is.
Here is the code for the LoginActivity:
public class LoginActivity extends AppCompatActivity {
@BindView(R.id.sign_in_button) SignInButton signInButton;
public GoogleSignInClient mGoogleSignInClient;
private int RC_SIGN_IN;
private static final String TAG = "Login Activity Error";
GoogleSignInOptions gso;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
sharedPreferences = getSharedPreferences(MainActivity.MY_PREFERENCES, Context.MODE_PRIVATE);
ButterKnife.bind(this);
signInButton.setSize(SignInButton.SIZE_STANDARD);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope("https://www.googleapis.com/auth/calendar.events"))
.build();
mGoogleSignInClient = GoogleSignIn.getClient(getApplicationContext(), gso);
findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
}
@Override
protected void onStart() {
super.onStart();
if (!sharedPreferences.getBoolean(MainActivity.SIGNED_IN, false)) {
signOut();
}
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
updateUI(account);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable 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);
updateUI(account);
} catch (ApiException e) {
updateUI(null);
}
}
private void updateUI(GoogleSignInAccount account) {
if (account != null) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(MainActivity.SIGNED_IN, true);
editor.apply();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra(MainActivity.PERSON_NAME, account.getDisplayName());
intent.putExtra(MainActivity.PERSON_EMAIL, account.getEmail());
intent.putExtra(MainActivity.PERSON_PHOTO, account.getPhotoUrl());
startActivity(intent);
}
}
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
}
}
Here is the relevant code for the MainActivity:
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setSupportActionBar(bottomAppBar);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
Intent intent = getIntent();
if (intent.getStringExtra(PERSON_NAME) != null) {
editor.putString("name", intent.getStringExtra(PERSON_NAME));
editor.apply();
}
if (intent.getParcelableExtra(PERSON_PHOTO) != null) {
editor.putString("photo", intent.getParcelableExtra(PERSON_PHOTO).toString());
editor.apply();
}
personName = sharedPreferences.getString("name", "");
personPhoto = Uri.parse(sharedPreferences.getString("photo", ""));
View headerView = navigationView.getHeaderView(0);
TextView navName = headerView.findViewById(R.id.nav_name);
CircleImageView navPicture = headerView.findViewById(R.id.nav_picture);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
menuItem.setChecked(true);
drawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case R.id.sign_out:
signOut();
break;
}
return true;
}
}
);
if (personName != null) {
navName.setText(personName);
}
if (personPhoto != null) {
Glide.with(this)
.load(personPhoto)
.into(navPicture);
}
}
private void signOut() {
SharedPreferences sharedPreferences = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(SIGNED_IN, false);
editor.commit();
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}