0

I'm doing an app similar to uber and in the sign up activity I have a checkbox where the user choose if wants to be a driver or a passenger. Due to the style of the app, I need that if the user choose to be a driver the app sets the activity "drivermain" as the launcher of the app. If choose passenger the launcher activity will be "passengerMain". Once the app is closed and re-launched it should evaluate if the user is a driver or passenger to send it to the right activity, as the app was closed it lost the status of the checkbox. I tried to do it comparing data through firebase, but I just got confused and stressed.

Hope help Thanks

  • Create a launcher activity like `SplashActivity` and then redirect the user to the appropriate activity according to your logic – Mosius Oct 19 '18 at 06:31

2 Answers2

3

I think you need something like this :

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

You will use the code above, after saving the type of app the user wants with this approach:

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

Teshte
  • 624
  • 1
  • 7
  • 26
  • 1
    yes, two things to remember: do not call setContentView cause it ads an unnecessary overhead to your application startup time. Also mark the `intent` variable as final to be sure it gets assigned once (in case your application startup gets bigger) – Zun Oct 19 '18 at 07:50
  • But it will just open another activity based on the check box status, if I launch the app once more, it won't have the status of the checkbox so it wont open an activity. I will edit the question to be clear – Jhoan Nicolas Andres Becerra Q Oct 19 '18 at 23:59
  • @JhoanNicolasAndresBecerraQ Well just after the user first opens the app, and selects the type of app that he wants (Driver or Passenger), you will need to save this preference somewhere..Edited answer on how to save his choice – Teshte Oct 20 '18 at 07:44
0

You have to store user choice in sharedpreference whether it is driver or passenger .

In your SplashActivity.class , Get user choice from sharedpreference and check

if Driver then

start drivermain.class

otherwise,

start passengerMain.classs

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51