components activation

tibburfrogtownMobile - Wireless

Dec 14, 2013 (3 years and 7 months ago)

105 views

components activation

activity: intent

Context.startActivity
()
startActivityForResult
()
Context.startActivity
()

startActivityForResult
()

service: intent

Context.startService
()

Context.bindService
()

broadcast receiver: intent

Context.sendBroadcast
()

Context.sendOrderedBroadcast
()

Context.sendOrderedBroadcast
()

Context.sendStrickyBroadcast
()

content provider

content resolver
component shutdown

CP, BR: active only while it’s responding
to a request
to a request

activity: finish(),
finishActivity
()

service:
stopSelf
(),
Context.stopService
()
intent filter

intent object can explicitly name a target
component.
component.

but
if a target is not explicitly named,
android must locate the best component
to respond to the intent(in
menifest
).

intent filters informs android of the kinds

intent filters informs android of the kinds
of intents the component is able to
handle.
activity and tasks

one activity can start another

including one defined in a different

including one defined in a different
application.

android maintains this user experience by
keeping both activities in the same task.
component lifecycle

activity lifecycle

active or running: foreground

active or running: foreground

paused: lost focus, but is still visible

stopped: no longer visible, often be killed

activity transitions and methods

void
onCreate
(Bundle
savedInstanceState
)

void
onStart
()

void
onRestart
()
void
onResume
()

void
onRestart
()

void
onResume
()

void
onPause
()

void
onStop
()

void
onDestroy
()
service lifecycle

Context.startService
(),
Context.stopService
()

Service.stop
(),
Service.stopSelfResult
()

Service.stop
(),
Service.stopSelfResult
()

Context.bindService
(),
Context.unbindService
()

method

void
onCreate
(): entire lifetime

void
onDestroy
(): entire lifetime
void
onStart
():
acticve
lifetime

void
onStart
():
acticve
lifetime
process and lifecycle

LRU(Least Recently Used)

5 levels in the hierarchy

5 levels in the hierarchy

foreground process

visible process

service process

background process

background process

empty
UI review
view hierarchy
layout

declaring layout

in an android app, the user

declaring layout

declare UI element in XML

instantiate layout elements at
runtime

XML resource load

in an android app, the user
interface is built using view
and
viewgroup
objects.

setContentView
()

activity must call to attach the
view hierarchy tree to the screen
for rendering

and pass a reference to the root
node object.

and pass a reference to the root
node object.
UI events

define an event listener and register it
with the view
with the view

override an existing callback method for
the view

event listener

onClick
(),
onLongClick
(),
onFocusChange
(),

onClick
(),
onLongClick
(),
onFocusChange
(),
onKey
(),
onTouch
(),
onCreateContextMenu
()
menu

define the
onCreateOptionsMenu
() or
onCreateContextMenu
() callback
mehods
onCreateContextMenu
() callback
mehods
for activity and declare the items that
you want to include in your menu.

android automatically create the necessary…
declaring layout

write the XML

each layout file must contain exactly one root element.

each layout file must contain exactly one root element.

ex) uses a vertical
linearLayout
to hold a
TextView
and a
Button

http://developer.android.com/guide/topics/ui/declaring
-
layout.html

load the XML Resource

XML layout files is
ocmpiled
into a View resource

should load the layout resource in
Activity.onCreate
() callback implementation

calling
setContentView
(), passing it the reference to your layout resource

calling
setContentView
(), passing it the reference to your layout resource

attributes

id, layout parameters, layout positions, size, padding, margins
creating menus

options, context, submenu

define a menu and all its items in an XML menu

define a menu and all its items in an XML menu
resource

http://developer.android.com/guide/topics/resources/menu
-
resource.html

inflating a menu resource

using
MenuInflater.inflate
()

onCreateOptionsMenu
() callback

android calls activity’s
onCreateOptionsMenu
()

override this method in activity

populate the menu that is passed into the method
other menu features

show of hide all items:
setGroupVisible
()

enable or disable all items:
setGroupEnabled
()

enable or disable all items:
setGroupEnabled
()

checkable all items:
setGroupCheckable
()

intents for menu items

setIntent
()
MenuItem
menuItem
=
menu.add
(0, PHOTO_PICKER_ID, 0, “Select Photo”);
menuItem.setIntent
(new Intent(this,
PhotoPicker.class
));
dialogs

create and displayed as a part of an activity

onCreateDialog
(
int
)

onCreateDialog
(
int
)

showDialog
(
int
)

onPrepareDialog
(
int
, Dialog)

http://developer.android.com/guide/topics/ui/dialogs.html

dismissing:
dismiss(),
dismissDialog
(
int
)

AlertDialog
:
extension of Dialog class

AlertDialog.Builder
(Context) subclass

AlertDialog.Builder
(Context) subclass

adding buttons: positive, neutral, negative

adding a list, etc…
handling UI events

event listener, event handler

interface in the View class that contains a single callback

interface in the View class that contains a single callback
method

called by the android when the View to which the listener has
been registered is triggered by user interaction with the item
in the UI

touch mode, handling focus

notifying the user
: toast, status bar, dialog