# CSE 340 Lab 6 (Spring 2020) ## Week 6: Bundles? Bundles! .title-slide-logo[ ![Android Logo](android-logo.png) ] --- # Timeline - Color Picker and Reflection due: Next Monday, May 11 @ 10:00pm - Lock: Wednesday, May 13 @ 10:00pm (if you are using late days) - Practice quiz for Accessibility and Color Picker will be out: this Friday, May 8 - Due: Next Wednesday, May 13 @ 10:00pm - **Reminder:** Please fill out this [form](https://docs.google.com/forms/d/e/1FAIpQLSdQrpZx-gexgDcKEF1SRp4egObimDP9qqVwLD56w0V2sYJDpw/viewform) by tomorrow night, so we can plan for our next assignment - Menus. --- # Section 6 Objectives - Solicit some anonymous feedback and questions - Bundles - What're they? - Why they're important - How we use them (for the assignment and otherwise) - Questions and work time on Color Picker --- # Feedback Form link: https://forms.gle/A7BBRGQGvF6J1aJ1A - Any questions you have about assignment/ examlet - Any feedback you have on section - What can we do better? What is going well? - We want this to be a good use of time for you. --- # Bundles: So you got lots of apps.. And they all want to use _tons_ of memory, but they don't **need** that memory all the time. - Android: You get no/minimal memory when you're not actively being used. - Apps: But then how do we remember stuff when we are being used if we can't save it in memory - Android: Use this `bundle` _(In truth the app could write/read its state to disk whenever it is being closed/opened but that is time consuming and would delay the OS launching new things)_ --- # What is a Bundle? - First what happens when the user closes the application? Does it die? - No! - Where does it go then? - Think about it as **hibernation** - All of it's memory is cleared, so all of your variables are _GONE_ 😲. - **But** Android lets you save some variables to a `bundle` right before your memory is cleared, and gives you your `bundle` back when you get the memory space back. - What's in the bundle? - You decide, entirely up to the application developer (you). _(Your bundle is destroyed if the user force quits the app through multitasking, or if the phone is turned off. There is also a max size of the bundle and likely not something you will run into)_ --- # Bundles & Code - Bundle management is occuring at the `activity` layer, not to be confused with any individual `view`. - When the user closes the app, right before it closes and its memory is cleared, Android invokes `onSaveInstanceState(Bundle outState)` on the activity, providing a reference to the activity for the app to save values into. - Then when the user opens the app again, Android invokes `onRestoreInstanceState(Bundle savedInstanceState)` returning the same bundle from earlier. - You can think of the `bundle` as a `Map
` that only supports certain types of objects (they must be `Serializable` which includes all primitives and `String`) _(Read more [here](https://developer.android.com/topic/libraries/architecture/saving-states))_ --- # 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!!!" } } ``` --- # Discussion: Using Bundles in Color Picker Not in this assignment but you could store the state of the Color Picker view directly into the bundle instead of storing the state of the application. - Think about the model of the application - Think about the model of the color picker view/interactor - Is it better to store the state of the application or the view? --- # Work time on Color Picker - Color-Angle conversion [hints](https://docs.google.com/document/d/1p97w2eoDe8hhmhDgE9fgkO5emimfKPy-eqGvzyaeUUQ/edit?usp=sharing)