firebase android onAuthStateChanged未被触发且结果代码始终为-1

时间:2017-02-11 15:49:39

标签: android firebase firebase-authentication

我有一个签名活动,我正在为我的应用使用Firebase auth和firebase UI。我的登录屏幕上有两个按钮Google +,另一个用于电子邮件密码组合。

我有两个问题

我的Auth状态已更改未触发,在我的onActivityResult中,我的结果代码始终为-1

当我关闭我的应用并重新安装/重新运行我的应用时,我将被带到一个空白屏幕。

请参阅下面的signinactivity

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;
}
}

迹:

我尝试调试我的代码,requestCode为1(正如预期的那样,但结果代码为-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

1 个答案:

答案 0 :(得分:-1)

qbix的回复有助于解决我的问题