android jam - activity lifecycle & databases - udacity lesson 4a

29
Android Manchester Week 4 - Activity Lifecycle & Databases

Upload: paul-blundell

Post on 15-Jul-2015

302 views

Category:

Technology


1 download

TRANSCRIPT

Android Manchester

Week 4 - Activity Lifecycle & Databases

Overview

● Retro

o Lesson 4a Activity Lifecycle & Databases basics

● Looking forward

o Lesson 4b ContentProviders

o Lesson 4c Loaders?

● Stickers + Booklets

● Next Tue 7-8pm SpacePortX

Energizer

Stand up and introduce yourself to

somebody around you, you don’t know

Prime Directive

‘Regardless of where we are up to, we

understand and truly believe that

everyone did the best job he or she

could, given his or her skills and

abilities, the personal time available,

and the situation at hand.’

Difficulty Check

● Who completed the class?

● How long did it take?

● Hands up 1-10 difficulty of the weeks lesson

● Any specific areas of the lesson that require particular

focus?

Retro Activity Lifecycle & Databases

What we learnt last week

● Why the Activity Lifecycle is important

● The Activity Lifecycle

● Data persistence methods in Android

● SQLite

● ContentProvider

● Testing basics

Lesson 4a - Activity Lifecycle● Why we need to know the Activity LifeCycle?

o Interruptions

o Memory issues

o Saving state

o User exits app

● Activity LifeCycle Methods come in pairs

o onDestroy - ?

o onStart- ?

o onResume - ?

● How often are the following methods called

o onResume?

o onStart?

o onCreate?

Lesson 4a - Answers

● Activity LifeCycle Methods come in pairs

o onDestroy() - onCreate

o onStart() - onStop

o onResume() - onPause

● How often are the following methods called

o onResume - multiple time

o onStart - multiple time

o onCreate/onDestroy - once

o onSavedInstanceState, onRestoreInstanceState

Lesson 4a - Lifecycle of an Activity

Lesson 4a - Termination of activity in onStop()

or

onPause()

pre-honeycomb

Lesson 4a - Activity LifeCycle

● Save state in onPause

or in onSavedInstanceState

● Restore in onCreate

or in onRestoreInstanceState

Activity Lifecycle Visibility

Difference between onStop/onStart & onPause/onResume

is visibility

● onPause for overlays

● onStop for background

UX of the exit button

- Android doesn’t have a concept of exit

- What does exit even mean

https://www.youtube.com/watch?v=631T7B8HOv4

Lesson 4a: Databases

● SharedPreferences Key-Value pairs for primitives -

covered last week

o Don’t work with complex data

o Can’t query

● Databases

o SQLite

Easier to Query, Update, Delete

Lesson 4a: Why Save Data

● Just fetch it from the cloud every time?

● Responsive (fast) application

● Responsible (sensible) application

Lesson 4a: Why Databases

● Efficient to find things

● Easy to organise

● Indexing

● Tables

VS

Lesson 4: Databases Overview

Three main components in the data storage methodology for this course

Contract

DbHelper

ContentProvider

Lesson 4a: Databases Helper

● Most important class

o Extends SQLiteOpenHelper

● onCreate()

o creates the tables using SQL query

o Called only the first time database is created.

● onUpgrade

o If the database updated you can call “Alter Table” SQL here to

ensure data is not lost during transition

● Other methods of note

o onDowngrade, getDatabaseName, getWriteableDatabase or

getReadableDatabase, onOpen, close.

Lesson 4a: Contract

● Define Table names & columns

o Also define URI helper methods

More on this next week.

● DbHelper will become unreadable if you had Col names, types, util

methods in an encompassing class.

● ContentProviders

o Hook to access database from anywhere

in the app in a safe manner

More next week!

Lesson 4a: Testing

● Convenient way of ensuring database is valid

o deals with edge cases

o Gives devs confidence in data validity

● TestSuite subclass specifies which tests to run

● AndroidTestCase subclass specifies x tests

● Test methods are prefixed with “test”

o i.e. testLocationInput

What went well

What went well

● Understanding the android lifecycle

● Restoring state

● Contract pattern in DB’s/ContentProvider

● Public Contracts (knowing what to do)o http://developer.android.com/reference/android/provider/ContactsContract.html

● Code written for you

● Testingo Saved a lot of time & pain

What didn’t go so well

What didn’t go so well

● Optional SQLite tutorial to read

● Code written for you

● Threading issues with the database

● Upfront talk of foreign keys & joinso didn’t understand why until much later

● Testingo caused a lot of time and pain

Q&A

Next Week - Android JamContentProviders

Next Lesson - Content Providers

Learning Objectives:- Creating a content providers

- URI, Uri Matcher, Uri Builder

- Bulk inserts

- Efficient updates.

See you next week..

- Time: Tuesday 7pm

- Place: SpacePort

- Things needed: You + Questions +

Feedback

- Can contact us if issues:

@blundell_apps / @android_mcr

Go forth and discuss