Vote count:
0
I'm a beginner to Android, so please bear with me. I'm using a CursorLoader in my Fragment class to move data from my content provider to my UI. Here's the method I use to put data into my content provider, which is in my MainActivity file:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
}
provider = new TestProvider();
providerInsert();
}
...
public void providerInsert() {
ContentValues values = new ContentValues();
values.put(TestContract.FIRST_COLUMN, 3);
values.put(TestContract.SECOND_COLUMN, "hello.");
final Uri uri = getContentResolver().insert(TestContract.CONTENT_URI, values);
}
provider was initiallized as a TestProvider(my implementation of ContentProvider) at the class level. Here's the loader I implement in the fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
getLoaderManager().initLoader(URL_LOADER, null, MainFragment.this);
ListView mListView = (ListView) rootView.findViewById(R.id.list_view);
mAdaptor = new SimpleCursorAdapter(
rootView.getContext(),
R.layout.test_list_item,
null,
mProjections,
mTo,
0
);
mListView.setAdapter(mAdaptor);
return rootView;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch(id) {
case (URL_LOADER): {
return new CursorLoader(
getActivity(),
TestContract.CONTENT_URI,
mProjections,
null,
null,
null
);
}
default: {
return null;
}
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdaptor.changeCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mAdaptor.changeCursor(null);
}
The loader works fine at first. The loader returns a Cursor that is set to the adapter, and the adapter does it's job, displaying the data into the ListView. However, when I rotate the screen, which triggers the onCreate() method of MainActivity, therefore adding new data to the content provider, shouldn't the loader send a new query? In other words, when I rotate the screen, shouldn't the onLoaderReset() and onCreateLoader() methods be called? That's what the Google documentation is telling me, anyways.
Thanks in advance! :)
onLoaderReset no being called
Aucun commentaire:
Enregistrer un commentaire