Mobile App Development 2022W Lecture 14
Video
Video from the lecture given on March 9, 2022 is now available:
Video is also available through Brightspace (Resources->Zoom Meetings (Recordings, etc.)->Cloud Recordings tab). Note that here you'll also see chat messages.
Notes
Lecture 14
----------
- I'll be grading midterms soon, so should be another week, sorry
- first want to release A3
- rest of term is on Android
Question for the day: why does textanalyzer-5 lose state when you rotate it,
but TapDemo doesn't?
- note that textanalyzer-5 keeps the text that you enter, it just
forgets the mode you're in.
So what's happening is the running activity is destroyed when you rotate the screen
- and textanalyzer-5 doesn't save the state properly
What's an activity?
- the unit of execution in an android app
- can have many activities
- when you run an app in any way, you are actually running a specific activity
- and of course there is a default activity that is run when
you launch the app
- when you have multiple screens, you can implement them as multiple activities
- but really, it is about splitting up functionality, not the interface per se
So in textanalyzer-5, the state variable analysisMode is forgotten because the activity was terminated and then restarted
- note that EditText preserves its own state after rotation
- but MainActivity doesn't, we have to implement that functionality
onCreate in an activity is a method that is called when an activity is created
- it has a single argument which is a Bundle
- bundles are used to store state that can be saved and restored
Basic idea with bundles
- when your activity will be terminated, save state into a bundle
- when your activity is created again, restore state from the given bundle
Bundles are dictionaries that can hold different types of data
- so key, value, with the value being able to have different types
- and the key is a string
- the Android runtime manages the instance state bundles,
we just have to put data in and pull data out at the right times
You should play with TapDemo and see when the different activity lifecycle methods are called
- when you rotate the screen, switch to another app, terminate an app, etc
So, why does Android terminate the activity when the screen is rotated?
- because the interface has to be redrawn anyway, easist way is to
kill the activity and start it again with new screen geometry
What about iOS apps? Do they have a lifecycle?
- yes definitely, much the same as Android
- however, by default much of the state management is handled automatically
- but you can change it
How can we fix TapDemo so that it shows the proper count after rotation?