Source:
<code>public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { <START> templateEditor = (CardTemplateEditor)getActivity(); <END> View mainView = inflater.inflate(R.layout.card_template_editor_item, container, false); final int position = getArguments().getInt("position"); try { final JSONArray tmpls = templateEditor.mEditedModel.getJSONArray("tmpls"); final JSONObject template = tmpls.getJSONObject(position); mFront = ((EditText) mainView.findViewById(R.id.front_edit)); mCss = ((EditText) mainView.findViewById(R.id.styling_edit)); mBack = ((EditText) mainView.findViewById(R.id.back_edit)); mFront.setText(template.getString("qfmt")); mCss.setText(templateEditor.mEditedModel.getString("css")); mBack.setText(template.getString("afmt")); TextWatcher templateEditorWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { try { template.put("qfmt", mFront.getText()); template.put("afmt", mBack.getText()); templateEditor.mEditedModel.put("css", mCss.getText()); tmpls.put(position, template); templateEditor.mEditedModel.put("tmpls", tmpls); } catch (JSONException e) { Timber.e(e, "Could not update card template"); } } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } }; mFront.addTextChangedListener(templateEditorWatcher); mCss.addTextChangedListener(templateEditorWatcher); mBack.addTextChangedListener(templateEditorWatcher); setHasOptionsMenu(true); } catch (JSONException e) { throw new RuntimeException(e); } return mainView; }</code><technical_language>a convenience function getModel() instead of storing this a member variable. cleaner handling lifetime issues. Also, please forget mVariableName naming convention for member variables</technical_language>

Target:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mTemplateEditor = (CardTemplateEditor)getActivity(); View mainView = inflater.inflate(R.layout.card_template_editor_item, container, false); final int position = getArguments().getInt("position"); TemporaryModel tempModel = mTemplateEditor.getTempModel(); final JSONObject template; try { template = tempModel.getTemplate(position); } catch (JSONException e) { Timber.d(e, "Exception loading template in CardTemplateFragment. Probably stale fragment."); return mainView; } mFront = ((EditText) mainView.findViewById(R.id.front_edit)); mCss = ((EditText) mainView.findViewById(R.id.styling_edit)); mBack = ((EditText) mainView.findViewById(R.id.back_edit)); mFront.setText(template.getString("qfmt")); mCss.setText(tempModel.getCss()); mBack.setText(template.getString("afmt")); TextWatcher templateEditorWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { template.put("qfmt", mFront.getText()); template.put("afmt", mBack.getText()); mTemplateEditor.getTempModel().updateCss(mCss.getText().toString()); mTemplateEditor.getTempModel().updateTemplate(position, template); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } }; mFront.addTextChangedListener(templateEditorWatcher); mCss.addTextChangedListener(templateEditorWatcher); mBack.addTextChangedListener(templateEditorWatcher); setHasOptionsMenu(true); return mainView; }