I am creating an app that requires the user to create a 4 digit password which is then used to login to the app in the future. How would I make it so that the app goes to the registration page if there is no password created, but goes to the login page if there is a password created. Does the startup activity need to be the registration page, the login page, or a different page? Thank you
5 Answers
You can use SharedPreferences
for this.
When you register an user store a value in SharedPreferences
and then every time you open the app asks for this value, if it's true then you go to LoginPage, if it's not go to RegisterPage
When the user registers to the app :
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putBoolean("userRegistered", true);
editor.apply();
Then everytime you launch your app do this :
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
Boolean isUserRegistered = prefs.getString("userRegistered", false); //False is a default value
if (isUserRegistered) {
startActivity(new Intent(this,Login.class));
}
else{
startActivity(new Intent(this,Register.class));
}
//Shorter way
startActivity(isUserRegistered ? new Intent(this,Login.class) : new Intent(this,Register.class));
You can do it in a SplashActivity
so you can do something like this guy Splash Activity example

- 19,464
- 18
- 81
- 148
-
Worked. Thank you – Mar 07 '19 at 17:16
Startup activity could be some sort of "Splash" activity. Just show app logo while getting all preparation stuff and deciding where to go next.

- 1,265
- 2
- 17
- 27
One possible approach is to save the information whether the user has or not the password using Shared Preference. When the activity start simply check that information and decide what page to load

- 110
- 7
Start with EditTexts for LoginId and password. Password can be saved in SharedPreferences. Below that include a Textview asking "First Time User?" with OnClick redirecting to a new Fragment or Activity for Registration.
Refer this article if you want the user to avoid feeding in the login credentials on each app launch.
https://medium.com/@prakharsrivastava_219/keep-the-user-logged-in-android-app-5fb6ce29ed65

- 43
- 11
I suggest you to use Fragments:
just include a container for fragment in your StartActivity, like this:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
Create two separate fragments for registration and for user interaction after registration. Then add a field, that will let you know if the user is registered or not:
SharedPreferences spref = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
Boolean isRegistered = spref.getString("userRegistered", false);
And now, just add the fragment you need (registration or interaction) into your container, according to isRegistered value:
FragmentManager myFragmentManager = getFragmentManager();
if (isRegistered) {
FragmentTransaction fragmentTransaction = myFragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.container, myFragmentRegistr, TAG_1);
fragmentTransaction.commit();
} else {
FragmentTransaction fragmentTransaction = myFragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.container, myFragmentInteract, TAG_1);
fragmentTransaction.commit();
}

- 582
- 2
- 8