This is my Fragment's Code:
EFragment
public class PreviewFragment extends Fragment
{
@Inject Bus bus;
@Inject HorstDb db;
//@ViewById(R.id.preview_text_view)
TextView previewTextView;
public PreviewFragment() {}
@Override public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
((HorstApp)getActivity().getApplication()).getComponent().inject(this);
}
@Override public View onCreateView(LayoutInflater inf, ViewGroup parent, Bundle args)
{
super.onCreateView(inf, parent, args);
View result = inf.inflate(R.layout.fragment_preview, parent, false);
previewTextView = (TextView) result.findViewById(R.id.preview_text_view);
return result;
}
@Override
public void onResume() { super.onResume(); bus.register(this); }
@Override
public void onPause() { super.onPause(); bus.unregister(this); }
@Subscribe
public void onTemplateSelected(TemplateSelected se)
{
Cursor c = db.getReadableDatabase().query("templates", null, "_id = ?",
new String[] { Long.toString(se.id) },
null, null, null);
if(c.moveToFirst())
{
previewTextView.setText(c.getString(c.getColumnIndex("content")));
}
c.close();
}
}
onTemplateSelected
will get called in onResume
via Otto. This works and previewTextView
will be non-null. The following version does not work, because previewTextView is null also I'am in the innermost lifecyle of an fragment and I am using @ViewsById. The difference is that I do not manually overload onCreateView
, instead I'm using @EFragment
and @ViewById
to get a reference to the TextView.
@EFragment(R.layout.fragment_preview)
public class PreviewFragment extends Fragment
{
@Inject Bus bus;
@Inject HorstDb db;
@ViewById(R.id.preview_text_view)
TextView previewTextView;
public PreviewFragment() {}
@Override public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
((HorstApp)getActivity().getApplication()).getComponent().inject(this);
}
@Override
public void onResume() { super.onResume(); bus.register(this); }
@Override
public void onPause() { super.onPause(); bus.unregister(this); }
@Subscribe
public void onTemplateSelected(TemplateSelected se)
{
Cursor c = db.getReadableDatabase().query("templates", null, "_id = ?",
new String[] { Long.toString(se.id) },
null, null, null);
if(c.moveToFirst())
{
previewTextView.setText(c.getString(c.getColumnIndex("content")));
}
c.close();
}
}
This is using the Fragment class from the Support library.
Any hints why the annotations don't work and previewTextView is null in the second example are welcome.