COMP 1601 2021W Tutorial 7 Solutions 1. Compare this code with the code from Lecture 15. What functional differences are there between the apps (other than what messages are logged)? Be sure to rotate the app fully (a two-click process in the simulator) and use it in both orientations. The key functional difference is the number of clicks is preserved when the device is rotated. The count is not shown, however, until the button is pressed after the click. 2. What activity lifestyle events happen when you do the following with the app: 2a. launch the app create, start, & resume 2b. terminate the app (from the app switcher) pause, stop, (state saved) - switching to the task switcher destroyed - terminating the app - note that state saving isn't an activity lifecycle event, it is something that is done before the app might be destroyed 2c. rotate the device (change device orientation) pause, stop, (state saved), destroy, create, start, resume 2d. run another app? pause, stop, (save state) 2e. switch back to the app? restart, start, resume 3. What code is responsible for saving the app's state? When is this code called, relative to the main activity's lifecycle? The onSaveInstanceState() method saves the app state. The actual saving is through the putInt calls made of the outState bundle. It seems to happen just after stop, thus before any destroy is called. 4. What code is responsible for restoring the app's state? When is this code called, relative to the main activity's lifecycle? State is restored in onCreate() on line 24, with count's value being restored with the return value of savedInstanceState.getInt(). 5. How is data organized in a bundle? Data is organized as a key/value store, with the keys being strings and the values being arbitrary types (i.e., doubles, ints, strings, or even arrays). 6. How is data saved to a bundle? Data is saved by calling put methods of the bundle, e.g., putInt(), supplying it a key (as a string) and the value to be saved. 7. How is bundle data loaded? Data is loaded by calling get methods of the bundle, e.g., getInt(), supplying it a key (as a string) and the return value being whatever had been saved previously. 8. How persistent is bundle-stored data? How can this be verified? The data is preserved only as long as the app is running. We can verify this by seeing how the count goes back to zero if we terminate and restart the app. 9. When rotating the app, currently it acts like the app has restarted even though the count is preserved. Change the app so that if the count is non-zero it will always display the right message. (It should behave like the iOS TapDemo from Lecture 16.) Your solution should avoid duplicate code. First, add this method to MainActivity: fun updateCountDisplay() { if (count > 0) { myMessage.text = "You clicked $count times." } else { myMessage.text = "You haven't tapped yet." } } Then, replace the myMessage.text assignment in myButtonPressed() with a call to updateCountDisplay(). Also, add a call to updateCountDisplay() to the end of onCreate(). (Note that to get the exact same behavior as the iOS TapDemo, remove the conditional and just do this: fun updateCountDisplay() { myMessage.text = "You clicked $count times." } The previous code preserves the original functionality while making it continue to work when the app is rotated.)