Search code examples
androidsqliteandroid-fragmentsandroid-intentandroid-cursorloader

How to reload CursorLoader after IntentService is executed


My main activity has a fragment

MainFragment mainFragment = new MainFragment(); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.layout_for_fragment, mainFragment).commit();

In onCreate method, calls an IntentService to fetch data from an API, then saves to SQLite in app side.

Intent intent = new Intent(this, AService.class); startService(intent);

The main fragment init a cursor loader to query the saved data from SQLiteDB, and show them in a listview.

getActivity().getSupportLoaderManager().initLoader(A_LOADER, null, this);

The cursor loader finishes before data is saved, so the list view is always empty.

How to make sure Service executes first? Or How to reload data after Service finished?


Solution

  • Resolved. Change to call initLoader when clicking a button.