The android activity lifecycle
-
Upload
eng-chrispinus-onyancha -
Category
Software
-
view
98 -
download
0
Transcript of The android activity lifecycle
![Page 1: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/1.jpg)
THE ANDROID ACTIVITY LIFECYCLE
![Page 2: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/2.jpg)
![Page 3: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/3.jpg)
Active and Visible Lifetimes
The onPause
The onResume
![Page 4: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/4.jpg)
![Page 5: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/5.jpg)
ACTIVITY TERMINATION
Starting from HoneyComb onwards, you can prepare for app termination in the onStop.
If you are targeting pre-honeycomb, prepare for it as early as onPause
![Page 6: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/6.jpg)
How to prepare for Termination
Close any connections or sockets
![Page 7: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/7.jpg)
![Page 8: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/8.jpg)
Maintaining State
onSaveInstanceState called immediately before onPause
onRestoreInstanceState is called immediately after onCreate but only if the app is being restarted after being terminated by the system
You can use these to restore your app the same state the user saw before their exited the app
![Page 9: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/9.jpg)
Storing Data in Android
Saves battery not to keep fetching data
Two Types:
1.SharedPreferences - using persistent key-value pairs for primitive data types
2.Databases - Helps organise and find data easily
![Page 10: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/10.jpg)
![Page 11: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/11.jpg)
![Page 12: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/12.jpg)
![Page 13: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/13.jpg)
![Page 14: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/14.jpg)
![Page 15: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/15.jpg)
![Page 16: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/16.jpg)
Contract
![Page 17: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/17.jpg)
![Page 18: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/18.jpg)
![Page 19: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/19.jpg)
![Page 20: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/20.jpg)
WEATHER AND LOCATION TABLES
![Page 21: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/21.jpg)
![Page 22: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/22.jpg)
SQLite OPneHelper
![Page 23: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/23.jpg)
![Page 24: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/24.jpg)
onUpgrade
Called when the database version has changed
Make sure to always change the version when you DB changes
If you modify the columns and add new ones, you might want to use the ALTER TABLE command to add missing columns to the new DB
![Page 25: The android activity lifecycle](https://reader034.fdocuments.in/reader034/viewer/2022051707/58ed391d1a28ab4e4f8b460f/html5/thumbnails/25.jpg)
Read/Writting to the DB
Writing.
1.Get a reference to the Writable database
2.Create a ContentValues object of what you want to insert
3.Insert the content values and get back the row ID
Reading4. All DB queries return cursor by default. A cursor is control structure that enables us
to traverse over the rows and columns of our result set