// Get data back from child activity. protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { return; } String id = data.getStringExtra(Constants.KEY_ID); String subject = data.getStringExtra(Constants.KEY_SUBJECT); String body = data.getStringExtra(Constants.KEY_BODY); String url = data.getStringExtra(Constants.KEY_URL); float rating = data.getFloatExtra(Constants.KEY_RATING, 0); boolean isWatched = data.getBooleanExtra(Constants.KEY_IS_WATCHED, false); // When returning from Editor Screen, add/update a movie in the list. if (requestCode == REQUEST_EDITOR) { if (id.equals(Constants.VALUE_NEW_MOVIE)) { // New Movie in list. Movie movie = new Movie(subject, body, url, rating, isWatched); mMovies.add(movie); // Add to file. FileManager.addToFile(this, FILE_NAME, movie); } else { int position = data.getIntExtra(Constants.KEY_POSITION, -1); // Update the movie in list at id position. Movie movie = mMovies.get(position); movie.setSubject(subject); movie.setBody(body); movie.setUrl(url); movie.setRating(rating); movie.setIsWatched(isWatched); // Rewrite the file. FileManager.saveFile(this, FILE_NAME, mMovies); } } // On returning from Search screen, add new movie in the list. else if (requestCode == REQUEST_SEARCH) { // New Movie in list. Movie movie = new Movie(id, subject, body, url); movie.setRating(rating); mMovies.add(movie); // Add to file. FileManager.addToFile(this, FILE_NAME, movie); } mMainListAdapter.notifyDataSetChanged(); }
// Transfer relevant data from Main activity to Editor activity. private void transferToEditorActivity(int position) { Movie movie = mMovies.get(position); Intent intent = new Intent(MainActivity.this, EditorActivity.class); intent.putExtra(Constants.KEY_ID, movie.getId()); intent.putExtra(Constants.KEY_SUBJECT, movie.getSubject()); intent.putExtra(Constants.KEY_BODY, movie.getBody()); intent.putExtra(Constants.KEY_URL, movie.getUrl()); intent.putExtra(Constants.KEY_RATING, movie.getRating()); intent.putExtra(Constants.KEY_IS_WATCHED, movie.getIsWatched()); intent.putExtra(Constants.KEY_POSITION, position); startActivityForResult(intent, REQUEST_EDITOR); }