android app programmingnowak/teaching/android/lecture02.pdf · 2018-11-06 · reactive programming...

Post on 27-Jun-2020

19 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Android App Programming

Unit 2: Databases, Runtime, UX Programming

Thomas NowakUniversité Paris-Sud

Last Time

Activities

• Single block of user interaction

• In the simplest case, one screen

• Can be a window or part of the screen

• To display something, set its content:

• setContentView(View)

Intents

• Mechanism to launch activities from other activities

• To change activity: need to create object of type Intent, then call the method startActivity with that object

• ex :Intent i = new Intent(this, OtherActivity.class); startActivity(i);

Bundles

• Mechanism to pass data to activities: Bundles

• Can put data in and retrieve it

• ex:bundle.putFloat(“pi”, 3.14); bundle.getFloat(“pi”);

Web services

• web service = set of responses from a web server that aren’t HTML

• return formats: typically JSON or XML

• query formats: often HTTP GET request w/ query strings

• ex: https://www.google.com/search?q=hi

• find web APIs: www.programmableweb.com

Databases

Saving State• several possibilities:

• passing bundles (ugh)

• write into a file (uuugh)

• databases

• preferencesSharedPreferences prefs = getPreferences(MODE_PRIVATE)

Databases• SQL databases:

• relational data model

• proven, robust, optimized

• NoSQL databases:

• different data model, e.g., objects, graphs

• less data translation necessary

Databases

• Physical location of data varies:

• locally (on the phone)

• remote (on one or more servers on the internet)

• e.g., Firebase

SQLite

• DBMS integrated into Android: SQLite

• packaged into the app

• runs in the same process

• no connection setup necessary

• use the class SQLiteDatabase included in Android

SQLite in Android• get instance of SQLiteDatabase via openOrCreateDatabase

• call db.rawQuery with SQL statement for results

• loop over result set with a Cursor

• for SQL statements without result, call db.execSQL

• there are specialized methods for some operations, e.g., db.insert

Android Runtime

Runtime• runtime = execution environment of programs

• implements the computational model of the programming language

• ex: C calling conventions

• call stack, parameter/return value passing, caller/callee saved registers

• ex: Python interpreter

• ex: Java Virtual Machine

Non-virtual Machines

ALU

R1 R2

R1

add R1, R2

else if(strncmp(op, "add", OP_LEN) == 0) { int *op1 = find_reg(arg1); int *op2 = find_reg(arg2); int *res = op1; *res = *op1 + *op2; program_counter++; } else if(strncmp(op, "sub", OP_LEN) == 0) { int *op1 = find_reg(arg1);

Virtual Machines

ALU

R1 R2

R1

add R1, R2add R1, R2

Virtual Machineselse if(strncmp(op, "add", OP_LEN) == 0) { int *op1 = find_reg(arg1); int *op2 = find_reg(arg2); int *res = op1; *res = *op1 + *op2; program_counter++; } else if(strncmp(op, "sub", OP_LEN) == 0) { int *op1 = find_reg(arg1);

Dalvik and ART

JIT in ART

Stack vs Heap• variables on the stack: implicitly destroyed after method

return (pop of stack frame)

• variables/constantes on the heap: destruction isn’t implicit; no temporal or logical ordering between objects on the heap

• error source in C++: managing the heap, in particular failure to destroy unused objects (memory leak)

• alternative of manually managing the heap: automatic garbage collection by the runtime

Garbage Collection• garbage collection = automatic identification and of

unused objects

• basic mechanism: identification of unused object based on active references

• choice of moment of collection

• goal: don’t block execution of the program

• additional challenge: avoid fragmentation of memory

Reference Counting

• idea: count the number of references to every object

• if count = 0, then destroy the object

• destroying one object can (transitively) cause destruction of a large number of other objects; problematic if immediate destruction

• problem with cyclic references

Mark-and-Sweep

• mark: calculate successors in the directed graph of objects and their references

• sweep: destroy all non-marked objects

• moment of collection:

• stop-the-world

• concurrent

Android Runtime

• originally Dalvik virtual machine

• now ART w/ AOT and JIT compilation of .dex files to native code

• runtime includes garbage collector (even outside of VM)

UX Programming

UX Design

Material Design

Constraint Layout

Fragments

Hamburger Menus

Notifications and Dialogs

MVC

MVP

MVVM

Reactive Programming

• reactive programming = asynchronous programming = callback methods = observer pattern

• manage streams of data

• well-adapted paradigm for asynchronous (UX) programs

• Android library: RxAndroid

Questions

top related