Lesson 4a: Lifecycle and Databases

Udacity – Developing Android Apps

Lesson 4a notes:

  • Why We Need an Activity Lifecycle
  • The Android Activity Lifecycle
    • A diagram of the Android Activity Life Cycle
  • Active and Visible Lifetimes
    • Lifecycle Events (Hint)
      • onPause
      • onStop
      • onDestroy
      • onCreate
      • onStart
      • onResume
    • To rotate
      • Linux: CTRL + F12
      • Mac: FN + LEFT CTRL +F12
      • Windows: LEFT CTRL + F11 or LEFT CTRL + F12
  • Activity Termination
    • onPause is called before onStop and should prepare the termination at that call.
  • How to Prepare for TerminationWhat To Do in OnPause/OnStop
    • Some examples of listeners or updates you should disconnect or stop when onPause or onStop are received.
      • Sensor Listeners
      • Location Updates
      • Dynamic Broadcast Receivers
      • Game Physics Engines
  • Maintaining State
    • Active > onSaveInstanceState > onPause > Terminated > onCreate > onRestoreInstantState
  • Bundles to Save App State
  • Storing Data in Android

SQLite Tutorial

  • SQLite Tutorial
    • Get SQLite
    • Create new db
    • list of comands
    • list databases
    • list table
    • find the schema of how the table is created
    • quit
sqlite3 database.db
.help
.databases
.tables
.schema
.quit
  • Create A Database Table
    • CREATE TABLE statement create a new database table
      • column definition is separated by commas (define column name and datatype)
    • SELECT statement return all rows base on the specific criteria. Use * for all rows.
CREATE TABLE weather( _id INTEGER PRIMARY KEY, date TEXT NOT NULL, 
min REAL NOT NULL, max REAL NOT NULL, humidity REAL NOT NULL, pressure REAL NOT NULL);
SELECT * from weather;
  • Insert rows
    • INSERT statement insert new row to the table. e.g.
  • Query rows
    • WHERE clause narrow down the number rows in the result.
    • Other operators
INSERT INTO weather VALUES(1, '20140625', 16,20,0,1029);
SELECT _id,date,min,max FROM weather WHERE date > 20140625 AND date < 20140628;
  • Update rows
    • UPDATE statement update existing rows information
  • Delete rowsDELETE statement delete the row from the table
UPDATE weather SET min = 0, max = 100 where date >= 20140626 AND date <= 20140627;
DELETE FROM weather WHERE humidity != 0;
  • Add columns
    • ALTER TABLE command to alter existing table.
  • Delete table
    • DROP TABLE command delete the table from the database
  • Example: return the max temperature row
ALTER TABLE weather ADD COLUMN description TEXT NOT NULL DEFAULT 'Sunny';
DROP TABLE weather;
SELECT * FROM weather ORDER

Lesson 4a notes cont…:

  • WeatherContract
    • To define an agreement between our data and our view describing how data will be stored.
    • Add package data to encapsulate our data model.
    • Add contract class to store the column information
    • Add entry to define the table and the data.
  • SQLiteOpenHelper help us handle database table
    • Add required method (CTRL-i)
    • Override constructor (CTRL-o)
      • Replace the String name to DATABASE_NAME, factor to null and version to DATABASE_VERSION
    • In onCreate method, create a string to create the weather table.
    • In onUpgrade method, delete the table and re-create the table
  • JUnit Testing
    • create com.example.android.sunshine.app.test package under src/androidTest/java
    • create a new class call FullTestSuite
    • create a new class call TestDb
    • Under app dropdown, add Android Tests and choose app in Module.
    • testInsertReadDb
      • Insert dummy data
      • Use dbHelper to get a writeable database
      • Create ContentValues object that store values and keys
      • Store dummy data in the columns from our LocationEntry contract
      • Insert the data into the db and verify we can get the row back
    • Cursor is the primary interface to the query results. It allow traversal of record in the database.
Advertisements