name: inverse layout: true class: center, middle, inverse --- # The Whole Toolkit Part III ## Storage Lauren Bricker CSE 340 Spring 23 --- layout:false [//]: # (Outline Slide) # Goals - Get input from the user (part 1) - Produce output for the user (part 2) - **How to store application specific data** (part 3) --- # The Whole Toolkit Architecture - Input - Input models (events) - Event dispatch - Event handling (state machine) - Callbacks to application - Output - Interactor Hierarchy design & use - Drawing models (`onDraw()`) - Layout (`onLayout()` or `XML`) - Damage and redraw process - **Storage** [more info](/courses/cse340/23sp/slides/wk03/storage.html#1) - Bundles - SharedPreferences --- # Storage options There are two main types different storage options to save data - - Short term volatile memory - Data that goes away when the phone is turned off - Long term memory - Data that can be recovered when the phone is turned off and back on --- # Short Term Storage option [Bundle](https://developer.android.com/reference/android/os/Bundle) - Usually stores a small amount of data - Saved RIGHT before the memory is cleared. - Typically used when you want to safely transfer data between activities or between an activity and a fragment. - The `Bundle` is sent back when the activity starts running again. - A `Bundle` is destroyed if the user **force quits** the app/phone is turned off. .footnote[More on [Activity state and ejection from memory](https://developer.android.com/guide/components/activities/activity-lifecycle#asem)] --- # What is in the Bundle? You, the application developer, decides what needs to be stored Values are mapped to unique KEYS (for set/retrieve) The `Bundle` is like a `Map
` that only supports `Serializable` objects (like primitives and `String`) --- # Bundles: Code Example ```java public abstract class MainActivity extends AppCompatActivity { @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("OUR_KEY", "bundles? bundles!!!"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); String whatWeSaved = savedInstanceState.getString("OUR_KEY"); // whatWeSaved would contain "bundles? bundles!!!" } } ``` --- # Long Term Storage Longer term, persistent storage stay around even if the phone is turned off. Shared Preferences - Store private primitive data in key-value pairs. Internal Storage - Store private data on the device memory. External Storage - Store public data on the shared external storage. SQLite Databases - Store structured data in a private database. Network Connection - Store data on the web with your own network server. .footnote[From [Android documentation](https://android-doc.github.io/guide/topics/data/data-storage.html)] --- # Long Term Storage Note that the app *could* write/read its state to longer term storage whenever it is being closed/opened ... -- ... but that is time consuming and would delay the OS launching new things... --- # Shared Preferences Stores data in persistent internal memory on the phone. Like `Bundle`, also stored in Key Value pairs [SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences) documentation --- # Shared Preferences To see what is in your shared preferences 1. start the Device File Explorer (View->Tool windows->Device File Explorer) 2. Use the toggles to open up the following folders ```` data data
for your app, such as cse340.askforhelp shared_prefs
.PREFERENCES.xml ``` --- # Shared Preferences Creating the Shared Preferences File ```java protected SharedPreferences getPrefs() { if ( mSharedPreferences == null ) { try { Context context = getApplicationContext(); mSharedPreferences = context.getSharedPreferences(context.getPackageName() + ".PREFERENCES", Context.MODE_PRIVATE); } catch (Exception e) { //failed to edit shared preferences file showToast(R.string.shared_pref_error); } } return mSharedPreferences; } ``` --- # Shared Preferences Saving to/restoring from the Shared Preferences file. Example (if we chose to store in SharedPreferences) ```java // setting data SharedPreferences.Editor editor = getPrefs().edit(); editor.putInt(CURRENT_TAB_ID_KEY, mTabId); editor.apply(); // have to force the write. // getting data, the last parameter is the default. mTabId = getPrefs().getInt(CURRENT_TAB_ID_KEY, 1); ``` --- # Shared Preferences Other types you can read/write ```java editor.putBoolean("key", booleanValue); editor.putInt("key", intValue); editor.putString("key", strintValue); editor.putStringSet(key, stringSetValue); ``` --- # END OF DECK