The question of passing data using intents
I am trying to modify the Notepad tutorial (Notepadv3 solution) to take the value into the NoteEdit class given by the Notepadv3 class. I have successfully used .putExtra in the OnListItemClick method to put the value into the NoteEdit class, but I am struggling to do that for the createNote method; When I try to create a new note, I get power.
I'm sure there is a simple solution, but I'm pretty new to Java and Android and really appreciate your help.
Here is the code in question in the Notepadv3 class:
private void createNote() {
Intent i = new Intent(this, NoteEdit.class);
i.putExtra("key", 1);
startActivityForResult(i, ACTIVITY_CREATE);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, id);
i.putExtra("key", 1);
startActivityForResult(i, ACTIVITY_EDIT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
And here is the receive code in the NoteEdit class:
mRowId = savedInstanceState != null ? savedInstanceState.getLong(NotesDbAdapter.KEY_ROWID)
: null;
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
: null;
value = extras.getInt("key");
}
I'm pretty sure (mRowId == null) is wrong when I use the createNote method, so getExtras won't get called. One of my questions would be how can I get the NoteEdit class to get this value when I use the createNote method? I'm new to this, so sorry if this is a simple question for all of you.
Thanks,
Joe
a source to share
When used Intent
to launch another activity, the receiving activity must use a method getIntent()
to access it; there is no reason to use for this purpose savedInstanceState
; which is only useful when "recovering" the current activity state because it was interrupted.
You can use "additional functions" Intent
in the send operation to transfer the data you want to send. The ingestion process uses the same additional names to access the data. Also, never assume all additional functions are dispatched (i.e. check null
s!)
You must make all action decisions in the onCreate
receiving activity method .
If an action can be "triggered" in more than one way, you should use Intent.setAction()
in Intent
and check all valid actions in the receiving process.
Also, independent of the parameter id
in onListItemClick
, this is often incorrect. The parameter is position
always correct and you can use it to navigate to the actual item from the adapter and get the ID from the item. Typically, one sets the Intent
data URI instead of sending the identifier as an optional one; this works better with Activity resolution (see below).
In fact, "data manipulation" actions must support standard data editing actions INSERT
, EDIT
etc., and have intent filters that define these actions and the MIME data type. I'm pretty sure they are part of the sample application.
In a receive activity you have to call Activity.setResult()
, and you usually send back the returned data (like a new content url) via another Intent
one that you then receive from the send activity in onActivityResult()
. In addition, you must call setResult()
at the appropriate time; for example calling it during lifecycle exit ( onPause()
or onStop()
) is too late and won't pass your results.
a source to share
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
: null;
if (mRowId == 0) mRowId = null;
value = extras.getInt("key");
}
Modify the code as follows. The problem is that the call to createNote creates a new note with mRowId as null. However, since you are passing the key argument, the additional parameters will not be null, but will have no value for KEY_ROWID, and getLong () returns 0 by default. The problem comes later when you ask for _id = 0 in populateFields ()
a source to share