Joe K 1973
Joe K 1973

Reputation: 75

Question about passing data using intents

I'm trying to modify the Notepad tutorial (the Notepadv3 solution) to take in a value into the NoteEdit class, given to by the Notepadv3 class. I've successfully used .putExtra in the OnListItemClick method to place the value into the NoteEdit class, but I'm struggling to do this for the createNote method; I'm getting a force close when I try to create a new note.

I bet there's a simple solution, but I'm pretty new to Java & Android and would very much appreciate your help.

Here's 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's the receiving 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 fairly sure that (mRowId == null) is not true when I'm using the createNote method so therefore getExtras won't be called. One of my questions would be how can I make my NoteEdit class get this value when I use the createNote method? I'm new to this so sorry if this is a simple question for you all.

Thanks,

Joe

Upvotes: 2

Views: 980

Answers (2)

Aleksi Niiranen
Aleksi Niiranen

Reputation: 358

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 like this. The problem is that calling createNote creates a new note with mRowId as null. However since you're passing the "key" argument the extras won't be null but neither will it have a value for KEY_ROWID and getLong() returns 0 as default. The problem emerges later when you're querying for _id = 0 in populateFields()

Upvotes: 0

escape-llc
escape-llc

Reputation: 1331

When using an Intent to start another activity, the receiving activity must use the getIntent() method to access it; there is no reason to use savedInstanceState for this purpose; that's only useful when "restoring" an activity's current state because it was interrupted.

You can use the "extras" of the Intent in the sending activity to transmit the data you want to send. The receiving activity uses the same extra names to get to the data. Also, never assume all the extras were sent (i.e. check for nulls!)

You should make all the decisions about what the activity's action is, in the onCreate method of the receiving activity.

If an activity can be "started" multiple ways, you must use Intent.setAction() in the Intent and check for all valid actions, in the receiving activity.

Also, do not depend on the id parameter in onListItemClick it is often incorrect. The position parameter is always correct, and you can use it to get to the actual item from the adapter, and get the ID from the item. Usually, one sets the Intent's data URI instead of sending the ID as an extra; this works better with the system's Activity resolution (see below).

In fact, "data manipulation" activities should support the standard actions for data editing, INSERT, EDIT and the like, and have intent filters that specify those actions and your data's MIME type. I'm pretty sure those are part of the example application.

In the receiving activity, you must call Activity.setResult(), and you typically send back return data (e.g. new content URL) via another Intent, which you then access from the sending activity in onActivityResult(). Also, you must call setResult() at an appropriate time; e.g. calling it during lifecycle exit (onPause() or onStop()) is too late, and will not transmit your results back.

Upvotes: 1

Related Questions