Mahesh N Mahesh N -4 years ago 180
Android Question

Firebase android onAuthStateChanged not fired and result code is -1 always

I have a signing activity and I am using Firebase auth and firebase UI for my app. I have two buttons on my login screen Google + and another for email password combination.

I have two problems

My Auth state changed is not firing and in my onActivityResult my result code is always -1

When I close my app and re-install/re-run my app, I am taken to a blank screen.

Please see signinactivity below

public class SigninActivity extends AppCompatActivity {

public static final String ANONYMOUS = "anonymous";
public static final int RC_SIGN_IN = 1;
private static final String TAG = "SigninActivity";

//private MessageAdapter mMessageAdapter;
private ProgressBar mProgressBar;

private String mUsername, loginUser;

// Firebase instance variables
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
private FirebaseDatabase mFirebaseDatabase;
private FirebaseStorage mFirebaseStorage;
private DatabaseReference mMessagesDatabaseReference;
private StorageReference mChatPhotosStorageReference;
private View mLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = findViewById(android.R.id.content);
mUsername = ANONYMOUS;
mFirebaseDatabase = FirebaseDatabase.getInstance();
// Initialize Firebase components
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseStorage = FirebaseStorage.getInstance();

// Initialize references to views
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);

// Initialize progress bar
mProgressBar.setVisibility(ProgressBar.INVISIBLE);

mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();;
Log.d("onAuthStateChanged", String.valueOf(user));
if (user != null) {
if (user.getDisplayName() != null) {
loginUser = onSignedInInitialize(user.getDisplayName());
} else {
loginUser = onSignedInInitialize(getString(R.string.email_user));
}
// User is signed in
} else {
// User is signed out
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setProviders(
AuthUI.EMAIL_PROVIDER,
AuthUI.GOOGLE_PROVIDER)
.setTheme(R.style.GreenTheme)
.build(),
RC_SIGN_IN);
}
}
};
}

@Override
protected void onStart() {
super.onStart();
mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}

@Override
protected void onStop() {
super.onStop();
if (mAuthStateListener != null) {
mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Log.d("requestCode", String.valueOf(requestCode));
Log.d("resultCode", String.valueOf(resultCode));
Log.d("data", String.valueOf(data));

if (resultCode == RESULT_OK) {
// Sign-in succeeded, set up the UI
Snackbar.make(mLayout, getString(R.string.signin_string),
Snackbar.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
} else if (resultCode == RESULT_CANCELED) {
// Sign in was canceled by the user, finish the activity
Snackbar.make(mLayout, getString(R.string.signin_cancel),
Snackbar.LENGTH_LONG).show();
finish();
}
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.sign_out_menu:
AuthUI.getInstance().signOut(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

private String onSignedInInitialize(String username) {
if (username != null) {
mUsername = username;
} else {
mUsername = getString(R.string.email_sign);
}
return username;
}
}


trace:

I tried debugging my code and requestCode is 1 (as expected, but the result code is -1)

02-11 10:34:19.609 4429-4429/App_Name D/requestCode:1

02-11 10:34:19.609 4429-4429/App_Name D/resultCode:-1

Answer Source

Response from qbix helped resolve my issue

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download