Many excellent applications on the market (for example, Sina Weibo, UC Browser) have adopted the Welcome page and the way to use the wizard to bring users a good user experience.
In general, the first time a user installs an app or installs a new version, it will appear as a welcome page-the way to use the wizard-main interface
The user does not have a new version installed or will be displayed as a welcome page when it is not first entered-the main interface way
To implement this different branch, we will use a variable to store whether we are entering the application for the first time, of course, this variable is not stored in the application, but is stored in the file under the application package name
So let's take a look at the steps for storing and modifying this variable.
1, in the application's Welcome page splash activity defines a variable isfirstin (splash in the previous article), the initial value does not matter, should be for us to read this value from the file immediately
2, define the Sharedpreferences object, and through the object to get the Isfirstin key (key) corresponding value (value), the default is true, because we did not create the file and the variable the first time we entered the application
3, in the splash need to jump where to do an if judgment, if Isfirstin is true to indicate that we are the first to enter, then jump to GuideActivity1, if False indicates that we are not the first time to enter, Then jump to the main page mainactivity
4, if GuideActivity1 is the last use of the wizard page, we want to GuideActivity1 after the end of the default jump to mainactivity, then we need to change before the jump Isfirstin value is false, coexist in the file, So the next time you enter the application, splash can get a value of false Isfirstin value from the file, so you can jump directly to mainactivity through the branch.
The following key sections of the code
Splash.java
Boolean isfirstin = false;
//oncreate
Sharedpreferences preferences = getsharedpreferences ("First_pref",
Mode_private);
Isfirstin = Preferences.getboolean ("Isfirstin", true);
New Handler (). postdelayed (New Runnable () {
@Override
public void Run () {
if (isfirstin) {
// Start guideactivity1
intent = new Intent (splash.this, guideactivity1.class);
} else {
//start tvdirectactivity
intent = new Intent (splash.this, tvdirectactivity.class);
}
splash.this.startactivity (Intent);
splash.this.finish ();
}
}, splash_display_lenght);
Guideactivity1.java before the end of the boot page must modify the value inside the sharedpreferences, so that the second entry will jump to the main page, do not enter the guide.
Sharedpreferences preferences = Getsharedpreferences (
"First_pref", mode_private);
Editor editor = Preferences.edit ();
Editor.putboolean ("Isfirstin", false);
Editor.commit ();
Finally, you can find the First_pref.xml file we defined in the app under the Shared_prefs folder under the app package name, open to see inside
This method of using Sharedpreferences to store variables can be used not only with the Welcome page and using the wizard, you can use it anywhere in the program, to store some key variables or values to be logged after the app exits
Android app displays the boot interface the first time it starts