25
THE ANDROID ACTIVITY LIFECYCLE

The android activity lifecycle

Embed Size (px)

Citation preview

Page 1: The android activity lifecycle

THE ANDROID ACTIVITY LIFECYCLE

Page 2: The android activity lifecycle
Page 3: The android activity lifecycle

Active and Visible Lifetimes

The onPause

The onResume

Page 4: The android activity lifecycle
Page 5: The android activity lifecycle

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

How to prepare for Termination

Close any connections or sockets

Page 7: The android activity lifecycle
Page 8: The android activity lifecycle

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

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
Page 11: The android activity lifecycle
Page 12: The android activity lifecycle
Page 13: The android activity lifecycle
Page 14: The android activity lifecycle
Page 15: The android activity lifecycle
Page 16: The android activity lifecycle

Contract

Page 17: The android activity lifecycle
Page 18: The android activity lifecycle
Page 19: The android activity lifecycle
Page 20: The android activity lifecycle

WEATHER AND LOCATION TABLES

Page 21: The android activity lifecycle
Page 22: The android activity lifecycle

SQLite OPneHelper

Page 23: The android activity lifecycle
Page 24: The android activity lifecycle

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

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