Step 3

In NoteEdit, we need to check the savedInstanceState for the mRowId, in case the note editing contains a saved state in the Bundle, which we should recover (this would happen if our Activity lost focus and then restarted).

1. Replace the code that currently initializes the mRowId:

mRowId = null;

Bundle extras = getIntent().getExtras();

if (extras != null) {

mRowId = extras.getLong(NotesDbAdapter.KEY ROWID);


with this:

mRowId = savedInstanceState != null ? savedInstanceState.getLong(NotesDbAdapter.KEY ROWID)

: null;

if (mRowId

== null)



extras =



= extras

!= null ? extras.getLong(NotesDbAdapter.KEY ROWID)


: null;

2. Note the null check for savedInstanceState, and we still need to load up mRowId from the extras Bundle if it is not provided by the savedInstanceState. This is a ternary operator shorthand to safely either use the value or null if it is not present.

0 0

Post a comment