简体   繁体   中英

How to show different page if user is first time user (Android)

I have a blank-ish Android Project and what I want to do is take the user to a different "page/screen" if it is their first time only .

I know the logic for this but since I'm new to Android, I'm unsure of how to code this.

Below are the steps I believe I need to take in order to accomplish this:

  1. App loads. If Local storage contains setting "FirstTimeUser", then it is not their first time using the app. Show the MainActivity page. If FirstTimeUser setting does not exist, it is their first time using the app (or they have uninstalled and reinstalled it), so instead, show WelcomeActivity page.
  2. After viewing Welcome activity page, create FirstTimeUser setting and set to False.

But how do I code this for an Android app?

Use shared preferences as shown:

//declare as global
SharedPreferences prefs = null;

//and in your onCreate method:
prefs = getSharedPreferences("packageNameHere", MODE_PRIVATE);

if (prefs.getBoolean("firstrun", true)) { 
    //do stuff here if first run

    //make sure to flag the boolean as false
    prefs.edit().putBoolean("firstrun", false).commit();
}
else{
    //if not first run, do something else
}

There is something called "SharedPreferences" that i believe your looking for.

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM