如何根据动作更改启动器活动

时间:2018-10-19 06:23:30

标签: java android database

我正在做一个类似于uber的应用程序,在注册活动中,我有一个复选框,用户可以在其中选择想要成为驾驶员还是乘客。由于应用程序的风格,我需要如果用户选择成为驱动程序,则该应用程序会将活动“ drivermain”设置为该应用程序的启动器。如果选择乘客,则发射器活动将为“ passengerMain”。一旦应用程序关闭并重新启动,它应该评估用户是将其发送到正确活动的驾驶员还是乘客,因为该应用程序关闭后,它失去了复选框的状态。 我试图通过Firebase比较数据,但感到困惑和压力。

希望帮助 谢谢

2 个答案:

答案 0 :(得分:3)

我认为您需要这样的东西:

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent;

    if (driver) {   // this is a boolean that you get from your Shared Preferences 
        intent = new Intent(MainActivity.this, DriverActivity.class);
    } else {
        intent = new Intent(MainActivity.this, PassengerActivity.class);
    }

    startActivity(intent);
    finish();
  }
}

使用此方法保存用户想要的应用程序类型后,您将使用上面的代码:

https://developer.android.com/training/data-storage/shared-preferences

答案 1 :(得分:0)

您必须将用户选择存储在 sharedpreference 中,无论是驾驶员还是乘客。

在您的 SplashActivity.class 中,从sharedpreference中获取用户选择并进行检查

如果是驱动程序,则

启动 drivermain.class

否则

开始 passengerMain.classs

相关问题