我刚刚开始使用FirebaseUI电子邮件/密码身份验证。
我的应用程序非常简单,到目前为止,它由1个活动组成,该活动使用TabLayout保存3个片段,如下所示:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
TabsAdapter adapter = new TabsAdapter(this, getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
在Firebase助手上,它表明我需要在onStart上检查用户是否已登录:
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
然后,助手显示如何注册并登录用户,但没有显示应该进入活动的哪一部分:onCreate
? onStart
?
*这是注册代码:
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
// ...
}
});
*这是登录代码
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
// ...
}
});
我很困惑,假设我当前使用的是上面提供的代码,并且工作正常,现在的工作流程是什么?我需要在Mainactivity中制作此注册/登录代码,然后代替Firebase助手提供的updateUI(null);
伪代码,而是仅使用firebase身份验证代码创建一个新的“主要活动”,然后使用意图将用户发送到当前的主要活动中,该活动将重命名为“ SignedInActivity”之类的东西,或者所有操作均在同一当前活动中完成?
**编辑:澄清我的意思: 我目前有MainActivity。 我需要:
1)将此当前MainActivity更改为SignedInActivity,然后将当前MainActivity替换为Firebase活动,然后在成功登录后将意图发送给SignedInActivity
或
2)用Firebase代码修改当前的MainActivity,以便我仍然拥有带有Firebase额外代码的当前MainActivity?
我也很困惑,所以希望我的帖子可以理解
答案 0 :(得分:3)
您确实应该查找Android Lifecycle。重要的是要知道何时调用回调(onCreate,onStart,onResume等),然后您可以自行决定在哪里实现代码。
在您的情况下,我将执行LoginActivity,您还可以在其中使用“ createUserWithEmailAndPassword()”注册新的配置文件,并使用已创建的帐户登录。如果成功,则可以启动MainActivity。
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
然后在MainActivity的onCreate回调中,您可以再次检查用户是否使用以下方式登录:
@Override
public void onCreate() {
super.onCreate();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null) {
updateUI(currentUser);
} else {
//intent back to login screen
}
}