I am trying to learn a few things from http://code.google.com/p/iosched/source/checkout. I wanted to see how they implemented that UI patterns they were talking about on the I/O.
In the HomeActivity they use this code to fire up the NotesActivity :
/* Launch list of notes user has taken*/
public void onNotesClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Notes.CONTENT_URI));
}
Notes class is in the ScheduleContract class and it looks like :
public static class Notes implements NotesColumns, BaseColumns {
public static final Uri CONTENT_URI =
BASE_CONTENT_URI.buildUpon().appendPath(PATH_NOTES).build();
public static final Uri CONTENT_EXPORT_URI =
CONTENT_URI.buildUpon().appendPath(PATH_EXPORT).build();
/** {@link Sessions#SESSION_ID} that this note references. */
public static final String SESSION_ID = "session_id";
/** Default "ORDER BY" clause. */
public static final String DEFAULT_SORT = NotesColumns.NOTE_TIME + " DESC";
public static final String CONTENT_TYPE =
"vnd.android.cursor.dir/vnd.iosched.note";
public static final String CONTENT_ITEM_TYPE =
"vnd.android.cursor.item/vnd.iosched.note";
public static Uri buildNoteUri(long noteId) {
return ContentUris.withAppendedId(CONTENT_URI, noteId);
}
public static long getNoteId(Uri uri) {
return ContentUris.parseId(uri);
}
}
I cant see what exactly this code do, and how it ends up starting NotesActivity with notes loaded in. I also dont understand how and for what is URI used as a second parameter in new :
Intent(Intent.ACTION_VIEW, Notes.CONTENT_URI).
I searched on Google for explanation but failed to find simple one and simple examples. I would guess that Notes class is used to point to and format the data (notes) and then somehow NotesActivity is started, but dont understand exactly how.
In Android you never launch a specific application, at least not directly. What you do, is that you create an
Intent, which isan abstract description of an operation to be performed:Whenever you want to launch another application, send a text message, pick a contact, activate the camera etc, you just create and start an
Intent, and then Android figures out by itself what application it should launch.So for your example with the Notes activity:
The first parameter,
Intent.ACTION_VIEW, tells that thisIntetwill display something to the user. The second parameter,Notes.CONTENT_URI, is the Uniform Resource Identifier for the Notes activity (in your example, the URI can also include an ID if you want to open the activity with a specific note). The result is that the Notes activity is displayed for the user.If you need more information, I suggest reading about Android Application Fundamentals and Intents and Intent Filters, which explain these concepts in detail