Notes for the study session 1.9.2012
"View, ViewGroups : Intents, Activities : Life Cycle"
======================
Views
Extends Object
Built in:
Button restart
Custom to Vivace:
SpriteCanvas spriteCanvas
"This class represents the basic building block for user interface
components."
"View is the base class for widgets, which are used to create
interactive UI components (buttons, text fields, etc.)."*
======================
ViewGroups
Extends View
Application ViewGroups:
MainLayout(Programmatic)
Preferences(XML)
The Base class for layouts
"A ViewGroup is a special view that can contain other views (called
children.)
The view group is the base class for layouts and views containers.
This class also defines the ViewGroup.LayoutParams class
which serves as the base class for layouts parameters."*
It can be defined with XML or with code. What are the pros and cons of
the two?
======================
Intents
Vivace uses intents to navigate from the Main screen to the
Preferences screen.
"An intent is an abstract description of an operation to be performed.
It can be used with startActivity to launch an Activity,"*
Intent intent = new Intent(spriteCanvas.getContext(),
Preferences.class);
// intent.putExtra("ballSize", String.valueOf(ballSize));
intent.putExtra("ballSize", String.valueOf((int) Ball.rad));
// startActivity(intent);
startActivityForResult(intent, 1);
======================
Activities
"An activity is a single, focused thing that the user can do. Almost
all activities interact with the user, so the Activity class takes
care of creating a window for you in which you can place your UI with
setContentView(View)."*
The example app. Vivace has two Activities:
Vivace
Preferences
The activity Vivacy is the main screen while, Preferences only
modifies a user preference.
The Activity Preferences takes a dotSize value and returns dotSize
value that the user has selected.
This Activity simply gets a value and returns a value, promoting
Activity independence.
======================
Object Independence With Events.
This is about Java not Android specifically.
I am only suggesting and experimenting with this in the context of
Android.
How else can this be done?
How do different classes communicate?
In Vivace the Sprites have a event system.
The Ball objects raise BallCaught events and scoring is done based on
these.
In the com.vivace.event package:
interface SpiteEventListener extends EventListener
class BallCaught extends EventObject
public BallCaught(Object source, int points) {
super(source);
this.points = points;
}
Inside Sprite:
public void setSpriteListener(SpiteEventListener listener) {
events.add(listener);
}
protected void trigger(EventObject evt) {
for (Object o : events) {
((SpiteEventListener) o).eventOccurred(evt);
}
}
Inside Vivace:
// b is the sprite 'Ball'.
b.setSpriteListener(new SpiteEventListener() {
@Override
public void eventOccurred(EventObject evt) {
if (evt.getClass().getSimpleName().equals("BallCaught")) {
// do something when the "BallCaught" event occurs
}
}
});
Inside Ball:
// trigger the event BallCaught with the points for this ball
trigger(new BallCaught(this, points));
// listen for the events internally
setSpriteListener(new SpiteEventListener() {
public void eventOccurred(EventObject evt) {
// do something when the event occurs
}
});
* Quotes from Google Reference Site:
http://developer.android.com/reference/