[Android instance] how to remember the status of the first Android startup, such as the boot page

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.