I have this code:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i(TAG, "The id of the selected note is " + id);
Intent editNote = new Intent(this, TaskEditActivity.class);
editNote.putExtra(TasksDBAdapter.KEY_ID, id);
startActivityForResult(editNote, EDIT_TASK_REQUEST);
}
And this code that retrieves the extra FROM A DIFFERENT ACTIVITY:
if (savedInstanceState != null) {
id = savedInstanceState.getLong(TasksDBAdapter.KEY_ID);
}
Log.i(TAG, "Id of note = " + id);
In the first code snippet, Logcat says: The id of the selected note is 2, but in the second code snippet, Logcat says: Id of note = 0. What just happened here? Any solutions to this VERY annoying problem.
I think you’re confusing the state which is saved when an
Activityis paused and the data delivered to theActivityvia anIntent.You want to have something like:
The
Bundlepassed toonCreate()is theBundleyou saved with theonSaveInstanceState()method and is not the extrasBundleyou added to yourIntent.