During development, it is often encountered that it is necessary to record whether the user started the app for the first time and give users some special prompts, such as help, which are reflected in the development of initialization and boot pages.
How to implement it first depends on the core technology, the objects and related methods we need.
First look at the code: Is the import and other things omitted here?
2.
The program is started for the first time after installation:
Start page --> function overview page --> system Homepage
Start later:
Launch page --> system Homepage
So you can judge it on the startup page.
You can save a file to a state. sharedpreferences is recommended.
1. You can define a variable to determine the number of times the program runs. If this is the first time, the system jumps to the guided activity. If it is not the first time, the system homepage is executed.
The code implementation for determining that the system is running for the first time is as follows:
Add code to activity:
// Use sharedpreferences to record the number of times the program is used
SharedpreferencesprefereNCEs;
@ Override
Publicvoidoncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Read the data required by sharedpreferences
Preferences = getsharedpreferences ("count", mode_private );
Int COUNT = preferences. getint ("count", 0 );
// Determine the program and the number of operations. If this is the first operation, the page jumps to the boot page.
If (COUNT = 0 ){
Intentintent = newintent ();
Intent. setclass (getapplicationcontext (), bootactivity. Class );
Startactivity (intent );
Finish ();
}
Editor editor = preferences. Edit ();
// Store data
Editor. putint ("count", ++ count );
// Submit the modification
Editor. Commit ();
The object we want to use here is sharedpreferences, which stores fixed data by operating the key-value pair, which is a bit of a sense of session, yes, you can use it as a part-time memory for session and so on. You can use getsharedpreferences to obtain the sharedpreferences object. mode_private is the mode for generating storage files. This mode indicates that only the application is allowed, and other applications are not allowed to access the configuration file. Next, you know, use int firststart = preference. getint ("firststart ",
0); To set an initial value. Then, use the editor object to modify the value. Then, you can determine whether the program is started for the first time based on the tag value at each startup. You can write all related services here.
Some people say this can be achieved. At the first startup, because there is no sharedpreferences file, it is the initialization value. For example, if true is to be displayed, if this value is set to false, the sharedpreferences file will be read at the next startup, if the value is found, the value is false. You just need to determine whether or not to display the boot interface.