---
layout: presentation
title: Lab 6 Slides
description: Bundling
---

# CSE 340 Lab 6 (Winter 2020)
## Week 6: Bundles? Bundles!

---

# Logistics and Agenda

- Solicit some anonymous feedback and questions
- Bundles
  - What're they?
  - Why they're important
  - How do we what we need to do (for the assignment and otherwise)
- Work time on color-picker

---

# Feedback

One side: any questions you have about assignment / midterm


Other side: any feedback you have on lab. What can we do better? What is going well? We want this to be a good use of
time for you.

(it doesn't matter which side is which)

---

# 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 **hiberation**


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<String, Object>` that only supports certain types of objects (they must be `Serializable` which includes all primitives and `String`)

_(docs [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!!!"
  }
}
```