03 b Addendum AndroidFrameworkElements Part2
03 b Addendum AndroidFrameworkElements Part2
1
Outline
• Activity Lifecycle
• Services
• Persistence
• Content Providers
2
Recap
• Android Framework
• Activities
• General UI:
– Layouts, Handler methods
– Widgets, Custom UI classes, Handling within
activity
• Specialized UIs:
– Menus and the Action Bar
– Special Activities – Preferences
• UI for larger screens: Fragments
3
The Activity Lifecycle
• Android runtime manages Activities
• Activities have a “lifecycle”
consisting of states: from creation
until death
• Standard (lifecycle) methods on the
activity are invoked at each state
change (can test by rotating device)
4
Activity States
• Created: Born to run
• Active: Working 9 to 5
• Paused: I’m about to break
• Resumed: Back to work
• Stopped: Obscured by clouds,
vulnerable
5
Activity Transitions
• Created ⟹ Active
• Active ⟺ Paused
• Paused ⟹ Stopped ⟹ Active
• Stopped ⟹ Killed
6
Timing of
Activity
Lifecycle
Methods
7
Lifecycle Methods
• onCreate(Bundle savedInstanceState): create
views, (re) initialize state
• onStart(): Restore transient state; one-time
processing
• onResume(): Session-specific processing, restore
transient state
• onPause(): Save persistent data, release resources,
quickly! Last method guaranteed to be called.
• onStop(): Called optionally by runtime
• onDestroy(): If finish() is called, or object is being
temporarily destroyed. Distinguish via
isFinishing(). 8
Transient State Management
Methods
• onSaveInstanceState(...): Called
before onPause(); use to save
transient state.
• onRestoreInstanceState(...):
Called after onStart() and
onResume(); use to restore state.
9
Interaction Across Activities
• Activity 1: onPause()
• Activity 2: onCreate(), onStart(),
onResume()
• Activity 1: onStop() – if it is
obscured
10
Outline
• Activity Lifecycle
• Services
• Persistence
• Content Providers
11
Services
• Activities for background, long-term processing (e.g.,
email, music, synchronization)
• Local: Accessible by a single app.
• Remote (aka bound): Accessible by all apps on the
device
– See: http://developer.android.com/guide/topics/fundamentals/
services.html#CreatingBoundService
• Comparison with threads:
– Coarser-grained processing
– Lifecycle separated from launching activity
– More resources consumed
– More respected by OS 12
Saving Persistent Data –
Files
• Same as in any Java or Kotlin app
• Internal storage (Linux file system)
– Local to app
– Destroyed upon uninstall
• External storage (e.g., SD card)
– Stored to SD card or flash memory (device-
dependent)
– Shared among applications
– E.g.: Java Kotlin
File imageFile = val imageFile =
new File(imageFilePath);
File(imageFilePath)
13
Cloud Persistence
• Cloud data stores useful when app users
need to read other users’ written data
• Example: Google’s Firebase
– Non-relational data store (high availability)
– Realtime Database: persists (key, value) data
in a shared JSON tree
– Cloud Firestore: supports more flexible
persistence (document-based approach)
– More info: http://firebase.google.com
14
Outline
• Activity Lifecycle
• Services
• Persistence
• Content Providers
15
Sharing Content – Content
Providers
• Standard content providers:
– Contacts, dictionary, media
• Referred to by URI prefix: content://
– Standard providers
• Permission must be requested in
manifest:
<uses-permission android:name=
"android.permission.READ_CONTACTS"/>
17
Summary
• Activities: “Single screen” of content that user sees
– Can be paused, resumed by users
– Managing Activity lifecycle is crucial!
• Services: Long-running tasks
• Persistence:
– Files (internal, external storage)
– (Local) SQLite database
– Look at https://developer.android.com/guide/topics/
data/data-storage.html for info about Room ORM (easier
to use than SQLiteOpenHelper)
• Content Providers: mechanism for sharing data 18
Credit
19