Unformatted text preview:

Notepad Exercise 3 In this exercise, you will use life-cycle event callbacks to store and retrieve application state data. This exercise demonstrates: • Life-cycle events and how your application can use them • Techniques for maintaining application state [Exercise 1] [Exercise 2] [Exercise 3] [Extra Credit] Step 1 Import Notepadv3 into Eclipse. If you see an error about AndroidManifest.xml, or some problems related to an Android zip file, right click on the project and select Android Tools > Fix Project Properties from the popup menu. The starting point for this exercise is exactly where we left off at the end of the Notepadv2. The current application has some problems — hitting the back button when editing causes a crash, and anything else that happens during editing will cause the edits to be lost. To fix this, we will move most of the functionality for creating and editing the note into the NoteEdit class, and introduce a full life cycle for editing notes. 1. Remove the code in NoteEdit that parses the title and body from the extras Bundle. Instead, we are going to use the DBHelper class to access the notes from the database directly. All we need passed into the NoteEdit Activity is a mRowId (but only if we are editing, if creating we pass nothing). Remove these lines: String title = extras.getString(NotesDbAdapter.KEY_TITLE); String body = extras.getString(NotesDbAdapter.KEY_BODY); 2. We will also get rid of the properties that were being passed in the extras Bundle, which we were using to set the title and body text edit values in the UI. So delete: 3. if (title != null) { 4. mTitleText.setText(title); 5. } 6. if (body != null) { 7. mBodyText.setText(body); } Step 2 Create a class field for a NotesDbAdapter at the top of the NoteEdit class: private NotesDbAdapter mDbHelper; Also add an instance of NotesDbAdapter in the onCreate() method (right below the super.onCreate() call):mDbHelper = new NotesDbAdapter(this); mDbHelper.open(); 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: 2. mRowId = null; 3. 4. Bundle extras = getIntent().getExtras(); 5. if (extras != null) { 6. mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID); 7. } with this: 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; } 8. 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. Step 4 Next, we need to populate the fields based on the mRowId if we have it: populateFields(); This goes before the confirmButton.setOnClickListener() line. We'll define this method in a moment. Step 5 Get rid of the Bundle creation and Bundle value settings from the onClick() handler method. The Activity no longer needs to return any extra information to the caller. And because we no longer have an Intent to return, we'll use the shorter version of setResult(): public void onClick(View view) { setResult(RESULT_OK);finish(); } We will take care of storing the updates or new notes in the database ourselves, using the life-cycle methods. The whole onCreate() method should now look like this: super.onCreate(savedInstanceState); mDbHelper = new NotesDbAdapter(this); mDbHelper.open(); setContentView(R.layout.note_edit); mTitleText = (EditText) findViewById(R.id.title); mBodyText = (EditText) findViewById(R.id.body); Button confirmButton = (Button) findViewById(R.id.confirm); 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; } populateFields(); confirmButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { setResult(RESULT_OK); finish(); } }); Step 6 Define the populateFields() method. private void populateFields() { if (mRowId != null) { Cursor note = mDbHelper.fetchNote(mRowId); startManagingCursor(note); mTitleText.setText(note.getString( note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE))); mBodyText.setText(note.getString( note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY))); } } This method uses the NotesDbAdapter.fetchNote() method to find the right note to edit, then it calls startManagingCursor() from the Activity class, which is an Android convenience method provided to take care of the Cursor life-cycle. This will release and re-create resources as dictated by the Activity life-cycle, so we don't need to worry about doing that ourselves. After that, we just look up the title and body values from the Cursor and populate the View elements with them. Step 7 Why handling life-cycle events is important If you are used to always having control in your applications, you might not understand why all this life-cycle work is necessary. The reason is that in Android, you are not in control of your Activity, the operating system is! As we have already seen, the Android model is based around activities calling each other. When one Activity calls another, the current Activity is paused at the very least, and may be killed altogether if the system starts to run low on resources. If this happens, your Activity will have to store enough state to come back up later, preferably in the same state it was in when it was killed. Android has a well-defined life cycle. Lifecycle events can happen even if you are not handing off control to another Activity explicitly. For example, perhaps a call comes in to the handset. If this happens, and your Activity is running, it will be swapped out while the call Activity


View Full Document

SMU CSE 7392 - Study Guide

Documents in this Course
Load more
Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?