gpt4 book ai didi

java - 如何将用户引导至其特定 Activity ?

转载 作者:行者123 更新时间:2023-12-02 11:20:21 25 4
gpt4 key购买 nike

我正在构建一个应用程序,当用户首次安装它时,在 MainActivity(启动器 Activity )中,应用程序会询问他是什么类型的用户(假设是司机还是骑手)?根据用户的选择,他被引导至相应的登录屏幕。用户登录后,他定向到 HomeActivity。如果他关闭应用程序而不注销,下次打开应用程序时,他应该直接看到 HomeActivity(基于他选择的用户类型)。对于一个用户来说,MainActivity 中的代码如下所示:

@Override
protected void onStart() {
super.onStart();

// Check if user is signed in (non-null) and update UI accordingly.
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
sendToStart();

}
}

private void sendToStart() {

Intent startIntent = new Intent(MainActivity.this,DriverHomeActivity.class);
startActivity(startIntent);
finish();
}

我是 Android 和 Firebase 新手。我不知道应该如何为两种类型的用户完成此操作。

最佳答案

创建一个登录屏幕,在该屏幕中用户可以选择工作类型(两个按钮或单选按钮)。

如果他选择Driver,则使用他编写的信息在数据库中创建一个Driver节点,并将他引导至主页 Activity :

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Drivers").child(user.getUid());
ref.child("name").setValue(name);
Intent startIntent = new Intent(MainActivity.this,DriverHomeActivity.class);
startActivity(startIntent);
finish();

对 Rider 也执行相同的操作。

<小时/>

如果用户关闭应用程序而不注销。在应打开的第一个 Activity (例如:启动 Activity )中,检查是否存在当前用户并检查用户类型:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 
DatabaseReference driver = FirebaseDatabase.getInstance().getReference().child("Drivers");
DatabaseReference rider = FirebaseDatabase.getInstance().getReference().child("Riders");

if (user != null) {
driver.child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Intent i = new Intent(SplashActivity.this, DriverHomeActivity.class);
startActivity(i);
finish();
} else {
Intent intent = new Intent(SplashActivity.this, RiderHomeActivity.class);
startActivity(intent);
finish();
}
@Override
public void onCancelled(DatabaseError databaseError) {

}
});

在启动 Activity 中,它将检查当前用户是否存在(如果用户已登录)user.getUid() 将返回当前用户 ID,然后检查该用户 ID 是否存在位于 Drivers 节点下,然后将其引导至驾驶员的主页 Activity。

关于java - 如何将用户引导至其特定 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49964294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com