This is a tutorial request to show a simple activity structure, where the user is prompted either to fill out a form, or if they have already done so in the past then continue to the main page.
The simplification here is to split your problem into the two categories. Create an Activity for your form, this is where the user can fill out their name address and whatever other details. Have a second Activity that is your ‘main’ Activity, this deals with a user once they have already filled out the resident details.
Now you want to control which of these Activities is seen by the user, new users will see the form and returning users will see your main. You guessed it, you can use a third Activity, the third Activity is the entry point of your application and this can decide which of the form or main is chosen.
Something like the below will make that decision. Ignore the AppSharedPreferences for now, but notice how we use an if else to control the flow of our application. This Activity has a loading spinner, but the check to see if they have filled the form is that fast you don’t even notice it.
package com.blundell.request.signupform; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import com.blundell.request.signupform.main.MainActivity; import com.blundell.request.signupform.signup.SignUpFormActivity; public class CheckEntryActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_check_entry); AppSharedPreferences preferences = AppSharedPreferences.newInstance(this); if (preferences.hasSignUpInformation()) { gotoMainActivity(); } else { gotoSignUpActivity(); } finish(); } private void gotoMainActivity() { Intent intent = new Intent(this, MainActivity.class); startActivity(intent); } private void gotoSignUpActivity() { Intent intent = new Intent(this, SignUpFormActivity.class); startActivity(intent); } }
The second half of this problem was knowing if the user has filled the form out or not, and as you can guess from the hints above you can use Android SharedPreferences for this. In this example project we have to use the same SharedPreferences instance in 3 different classes and I would always recommend encapsulating this duplication in one class. This also allows us to have meaningful names for our methods i.e. we have hasSignUpInformation() vs activity.getDefaultPrefs().hasKey(“signUpInfo”);
Saving to your preference file you can keep a track of if the user has entered all the information already. You can also use this information later on to make your approach more friendly. “Welcome Paul”.
package com.blundell.request.signupform; import android.content.Context; import android.content.SharedPreferences; public class AppSharedPreferences { private static final String PREFERENCES_NAME = "AppSharedPreferences"; private static final String KEY_SIGN_UP_NAME = "AppSharedPreferences.SIGN_UP_NAME"; private final SharedPreferences sharedPreferences; public static AppSharedPreferences newInstance(Context context) { SharedPreferences sharedPreferences = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE); return new AppSharedPreferences(sharedPreferences); } AppSharedPreferences(SharedPreferences sharedPreferences) { this.sharedPreferences = sharedPreferences; } public void saveSignUpName(String signUpInfo) { sharedPreferences.edit().putString(KEY_SIGN_UP_NAME, signUpInfo).apply(); } public boolean hasSignUpInformation() { return getSignUpName() != null; } public String getSignUpName() { return sharedPreferences.getString(KEY_SIGN_UP_NAME, null); } public void clearSignUpInformation() { sharedPreferences.edit().remove(KEY_SIGN_UP_NAME).apply(); } }
Thats it! As long as you split your functionality into simple to understand components then it always makes writing apps simpler. Just try and have one Activity with one job, you can also use Fragments if your UI is becoming too complex. One final note from the example project, always do your Intent navigation from an Activity NOT from a Fragment, this will help you later on when dealing with phones vs tablets vs watches.
The source code is available here on GitHub.
The main components being
– CheckEntryActivity
– SignUpFragment
– MainFragment
– AppSharedPreferences
Comments or Questions see below