Always prepare and launch intents from a separate method and name those methods consistently. The code to launch an intent usually requires minor changes and
parameter tweaks as the project progresses. It changes frequently, and often all
the intent code is looked at together. Mixing it in next to other code causes
nothing put lingering, hard to debug, headaches.
So instead of what is shown in the Notepad tutorial (part 2) fragment for OnListItemClick():
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = mNotesCursor;
c.moveToPosition(position);
Intent i = new Intent(this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, id);
i.putExtra(NotesDbAdapter.KEY_TITLE, c.getString(c
.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
i.putExtra(NotesDbAdapter.KEY_BODY, c.getString(c
.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
startActivityForResult(i, ACTIVITY_EDIT);
}
Do this instead:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = mNotesCursor;
c.moveToPosition(position);
intent_edit_for_result(id, c.getString(
c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)),
c.getString(c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
}
protected intent_edit_for_result(long, rowId, String title, String body) {
Intent i = new Intent(this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, rowId);
i.putExtra(NotesDbAdapter.KEY_TITLE, title );
i.putExtra(NotesDbAdapter.KEY_BODY, body );
startActivityForResult(i, ACTIVITY_EDIT);
}
The performance cost is minimal, mistakes are easier to see, testing is easier, and you won't cringe when need to review all your intent related code.