How can I use startmanagingcursor in fragment? Because I need to pull data from SQLite in Fragment but I cannot use startmanagingcursor in it.
Here is my coding
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbUtil.open();
Cursor cursor = dbUtil.fetchNews(getArguments().getString("title"));
getActivity().startManagingCursor(cursor);
newsTitle = cursor.getString(0);
dbUtil.close();
}
Since everyone else seems to want to preach at you and not give you an answer, I’ll actually answer your question (then preach :p).
It’s as simple as that.
As others have noted, it’s deprecated and a
CursorLoaderis recommended for many reasons… keeping data processing off the UI thread being the major one.But if you truly need/want to use
startManagingCursorin a fragment, the above snippet is how you would do it.Given the further information you have posted, another issue might be where you are trying to call your database from. You should not use the
onCreatemethod in a fragment class (at least for these operations), you should useonActivityCreated. That way you are certain that the activity that controls your fragment has finished setting itself up before you try and use anything associated with it.