1 of 41

The Lifecycle

Android vs Java

2 of 41

If you use Android Studio on the lab machines: Update Java JDK Location

  • Right click on app folder
  • “Open Module Settings”
  • Un-check “Use Embedded JDK (recommended)”
  • Change location to /usr/local/oracle_jdk
    • No way to configure this automatically, unfortunately

3 of 41

HW 1 Recap

  • Who got it working?
  • Who had errors?
  • Common Errors:
    • App could not be loaded without app.iml file
    • Platform hash string android-22 could not be found
    • Build tools 22.0.0.1 could not be found
    • Unsupported major minor version 52.0.0

4 of 41

Running a Java Program

5 of 41

Compiling and Running Java Programs

6 of 41

7 of 41

What is “main” all about?

  • public static void main(String[] args) ?

8 of 41

$ man java

9 of 41

Where is main in our MainActivity.java?

10 of 41

The Android Lifecycle

11 of 41

Android Runs and Drives our Code

  • No single entry point for a program
  • Android drives your app via callbacks
  • Every piece of code that runs on Android must respect the lifecycle
  • Goal is resource management
    • Give the platform control

12 of 41

android.app.Activity

  • Building block of user-facing Android programs
  • “Single, focused thing a user can do”
  • Can set a UI
    • setContentView()
  • Usually is full-screen

Generally speaking, an Activity can be thought of as a single screen of an app.

13 of 41

Activity Lifecycle

  • Resumed
  • Paused
  • Stopped

14 of 41

Activity Lifecycle States

  • Resumed
    • Foreground, interactive
  • Paused
    • Foreground but blocked, not interactive
  • Stopped
    • In the background, can be killed

15 of 41

Things that affect the Lifecycle

  • Apps coming to the foreground (e.g. phone call)
  • Rotating the screen
  • System killing apps for resources
  • In-app navigation (e.g. clicking on an email)
  • Outside navigation (e.g. back and home button)
  • Etc: everything on the phone affects the lifecycle

16 of 41

17 of 41

Other Lifecycles. Common Paradigm

  • Services
  • Fragments
  • Views (a bit less so)
  • etc

18 of 41

Deep Dive on Lifecycle

19 of 41

onCreate(Bundle bundle)

  • Most initialization goes here
  • Every Activity has to implement it
  • Set UI via setContentView(int resourceId)
  • Set touch listeners for UI elements
  • Start loading data
  • bundle only non-null if restoring from previous Activity
    • More later

20 of 41

onStart()

  • Called when Activity becoming visible

21 of 41

onResume()

  • Your Activity is at the top of the Activity stack
  • User input going into activity
  • Always followed by onPause()

22 of 41

onPause()

  • Called when system about to start resuming previous Activity
  • Commit unsaved changes
  • Stop animations
  • Stop loaders, tasks, other things consuming CPU
    • Respect the lower priority your Activity is taking
  • Recommended to be quick, as next Activity can’t start

23 of 41

onStop()

  • Activity no longer visible
  • Activity is KILLABLE
    • activity may be killed by the system at any time without another line of its code being executed
  • No guarantee this will be invoked
    • ….so what good is it?

24 of 41

onDestroy()

  • Last callback you receive
  • Activity is KILLABLE
    • activity may be killed by the system at any time without another line of its code being executed
  • No guarantee this will be invoked
    • ….so what good is it?

25 of 41

Saving Instance State

  • Android tries to save Activity state
  • Provides better user experience during normal use
    • Phone call, reclaim memory without losing state/progress
  • Only saves state if expected to return
  • Back button does not save state
    • This makes sense
    • But this also increases the number of states you have to worry about

26 of 41

Saving State in Lifecycle

27 of 41

android.os.Bundle

  • Typed Map
  • Messaging object
  • Passes data all around the Android OS
  • putInt(String key, value int); getInt(String key)
  • putString, putByte, putChar, putIntegerArray, put*(String key, * value), etc

28 of 41

onSaveInstanceState(Bundle outState)

  • Save UI state not handled by system
  • When restoring, onCreate() and onRestoreInstanceState() receive same bundle
    • Typically onCreate() will be enough, and you don’t need onRestoreInstanceState()

29 of 41

30 of 41

Lifecycle in Action: log statements

($ git checkout lifecycle-logs)

  • onCreate
  • onStart
  • onResume
  • BACK
  • onPause
  • onStop
  • onDestroy

31 of 41

32 of 41

Now that we understand the lifecycle, how we you use it?

How do we tell Android to start an Activity for us?

33 of 41

Creating an Activity

  • MainActivity mainActivity = new MainActivity();
    • WRONG WRONG WRONG

  • Instead we tell Android what we want
  • Android giveth.

34 of 41

android.content.Intent

  • Intent is a message
  • Android handles the intent and delivers it to the appropriate app
  • An Intent can:
    • Start an Activity
    • Start a Service
    • Broadcast a message to all apps configured to listen

35 of 41

Creating an Intent

  • Explicit: Start a specific Activity
    • Via a ComponentName(“org.cse390.playground”, “MainActivity”)
  • Implicit: Let Android resolve the Activity
    • Eg ACTION_EDIT, content://contacts/people/
    • Android will open a picker for Contacts apps

36 of 41

Programmatic Intents ($ git checkout intent-basics)

37 of 41

Intents are handled by the system

38 of 41

Specifying an Initial Activity

  • Tell the system what to do when your app icon is clicked
  • Update AndroidManifest.xml
  • Add <intent-filter>
  • action = android.intent.action.MAIN
  • category = android.intent.category.LAUNCHER

39 of 41

AndroidManifest.xml

40 of 41

Questions?

  • A lot of info to throw at you
  • Have to move through the basics quickly

41 of 41

HW 2: Build public Android repo

  • Complete HW 1!!! (i.e. set up Android Studio)
    • I am going to be tricky with this in future assignments
    • Best to get it sorted now
  • Find an open source Android project
    • Build it locally and run on device/emulator
    • A lot of variation here. No real success/failure
    • Look for email with submission details
  • Play around with playground repo if you’re so inclined