Interpretation of Activity 4-Part1

Source: Internet
Author: User

In this section, we will discuss the fifth topic, which is actually about data storage in Android. Let's take a look at the Common Data Storage Methods: file storage and database storage are common methods, and these storage methods are also provided in Android. Of course, as previously introduced, there are two main categories, one is document-type data, which is mainly stored in files or databases, the second type is the storage of configuration files (such as the configuration file for the calendar view: weekly view or daily view), which are implemented by Preference in Android. Of course, our storage here is not only a general understanding of the concept of store, but also includes getting the data and performing some operations on the data. Another point is that the processes in Android are independent of each other, which involves data sharing. For example, the data of contacts is shared by many applications, such as phones and text messages! So how does Android achieve data sharing among multiple processes? This is the Content Provider of one of the four Android components. Therefore, there are many contents related to this section. We can record them in the following three parts!

Part1: Describes Preference storage;

Part2: introduces SQLite and File storage;

Part3: introduces Content Provider;

Naturally, we first think of getting SharedPreferences and then performing operations. The following two interfaces are used:

 

Now we have a clear idea. We can use the Context .. getSharedPreferences (String name, int mode) method to obtain the SharedPreferences interface and then use its internal Editor interface to operate the data.

Detailed descriptions of the Context.. getSharedPreferences (String name, int mode) method are as follows:

 

The second parameter is clear at a glance as described above, namely the operation mode, readable and writable. The first parameter, Pregerences, is used to operate data in an XML file, and the content in the XML file is saved as a key-value pair, the first parameter is the name of the XML file. If this file does not exist, when you call the Editor interface to obtain or operate the elements, the file will be created, and then the Editor interface's commit () will be called () method to submit and save the changes. Www.2cto.com

There are some code snippets in the official documents that play a very important role in our thinking. Here we will extract them as follows:

Public class CalendarActivity extends Activity {
...
 
Static final int DAY_VIEW_MODE = 0;
Static final int WEEK_VIEW_MODE = 1;
 
Private SharedPreferences mPrefs;
Private int mCurViewMode;
 
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
 
SharedPreferences mPrefs = getSharedPreferences ();
MCurViewMode = mPrefs. getInt ("view_mode", DAY_VIEW_MODE );
}
 
Protected void onPause (){
Super. onPause ();

SharedPreferences. Editor ed = mPrefs. edit ();
Ed. putInt ("view_mode", mCurViewMode );
Ed. commit ();
}
}
Public class CalendarActivity extends Activity {
...

Static final int DAY_VIEW_MODE = 0;
Static final int WEEK_VIEW_MODE = 1;

Private SharedPreferences mPrefs;
Private int mCurViewMode;

Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

SharedPreferences mPrefs = getSharedPreferences ();
MCurViewMode = mPrefs. getInt ("view_mode", DAY_VIEW_MODE );
}

Protected void onPause (){
Super. onPause ();
 
SharedPreferences. Editor ed = mPrefs. edit ();
Ed. putInt ("view_mode", mCurViewMode );
Ed. commit ();
}
} The above Code first obtains the SharedPreferences interface, and then obtains the value of the view_mode field. When the Activity is paused, call the putInt () method in the Editor interface to save the value of the view_mode field, this prevents data loss when the Activity is not in the running status.

Here, we also use a simple example to test our idea: a text edit box is displayed on the Activity, where users can enter text, when you exit this activity and enter this activity again, the data will still appear on this activity!

Step 1: Create a new project, Preferences;

Step 2: Modify the layout file and place an EditText component in the text editing box above;

Step 3: Modify the java source file and add some code, which is similar to the preceding sample code, as shown below:

Private EditText editText;
Private static final String TEST = "test ";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

EditText = (EditText) findViewById (R. id. EditText01 );
SharedPreferences pre = this. getSharedPreferences (TEST, MODE_WORLD_READABLE );
EditText. setText (pre. getString ("text ",""));
}
 
@ Override
Public void onStop (){
Super. onStop ();
SharedPreferences. Editor editor = this. getSharedPreferences (TEST, MODE_WORLD_READABLE). edit ();
Editor. putString ("text", editText. getText (). toString ());

Editor. commit ();
}
Private EditText editText;
Private static final String TEST = "test ";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

EditText = (EditText) findViewById (R. id. EditText01 );
SharedPreferences pre = this. getSharedPreferences (TEST, MODE_WORLD_READABLE );
EditText. setText (pre. getString ("text ",""));
}
 
@ Override
Public void onStop (){
Super. onStop ();
SharedPreferences. Editor editor = this. getSharedPreferences (TEST, MODE_WORLD_READABLE). edit ();
Editor. putString ("text", editText. getText (). toString ());

Editor. commit ();
} The code is almost the same as the sample code above, so I will not explain it!

Step 4: Check the running effect. When the program is run for the first time, an empty edit box is displayed. This is because the default value of the text field is empty, so it is not displayed at the beginning, at this time, we enter a line of strings above and exit the program. At this time, the onStop () method will be called to save the string you just edited and run the program again, we will call the onCreate () method to obtain the value of the text field and display it in the edit box, that is, to obtain and display the string we edited last time! Here we will first look at the effect:

 

Next let's take a look at whether the file system of the running simulator contains the test. xml file we created, which can be viewed using the eclipse file browser or using the adb to connect to the simulator:

 

Okay. Come here first!

From the column chenlong12580

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.