Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of storing data in Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the method of storing data in Android". In the daily operation, I believe that many people have doubts about the method of storing data in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of storing data in Android?" Next, please follow the editor to study!

Android provides a variety of ways to store data, the simplest of which is to use Shared Preferences. Shared Preferences can store Key/value pairs, Shared Preferences supports access to boolean, float, long, integer, string, the most commonly used Shared Preferences is to store some application preferences. Another way is to use onSaveInstanceState (), which is specifically used to save the UI state.

App- > Activity- > Persistent State uses Shared Preferences to maintain part of the UI state (the value of TextView).

Create or modify a SharedPreferences, using the getSharedPreferences (String name, int mode) method. Shared Preferences is used to share some data between different Activity of a single Application, but it cannot be used to share data between different Application.

SharedPreferences.Editor is used to add data to SharedPreferences: editor.putXXX (key,value)

Protected void onPause () {super.onPause (); SharedPreferences.Editor editor = getPreferences (0). Edit (); editor.putString ("text", mSaved.getText (). ToString ()); editor.putInt ("selection-start", mSaved.getSelectionStart ()); editor.putInt ("selection-end", mSaved.getSelectionEnd ()); editor.commit ();}

Read Shared Preference: pref.getXXX (key)

Protected void onResume () {super.onResume (); SharedPreferences prefs = getPreferences (0); String restoredText = prefs.getString ("text", null); if (restoredText! = null) {mSaved.setText (restoredText, TextView.BufferType.EDITABLE); int selectionStart = prefs.getInt ("selection-start",-1); int selectionEnd = prefs.getInt ("selection-end",-1); if (selectionStart! =-1 & & selectionEnd! =-1) {mSaved.setSelection (selectionStart, selectionEnd) }

Persistent State demonstrates how to use Shared Preferences to preserve the contents of EditText during Activity recovery. A more general approach is to use onSaveInstanceState.

At this point, the study on "what is the method of storing data in Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report