I'm making an application which shows a list of zipcodes( in ListView) and when I click on it, it shows me detailed weather report using OpenWeather API. The list of zipcodes are stored in SQLite database and to retrieve data from it, I'm using AsyncTaskLoader. The data is shown properly. The problem is during configuration change, loadinBackground() method is being called which shouldn't happen.
public class DataLoader extends AsyncTaskLoader<List<RowItem>>
{
Context activityContext;
private String TAG="DataLoader";
public DataLoader(Context context)
{
super(context);
this.activityContext = context;
}
@Override
public List<RowItem> loadInBackground()
{
Log.v(TAG, "loadinBackground() IN");
List<RowItem> rowItems = new ArrayList<RowItem>();
DbReaderHelper mDbHelper = new DbReaderHelper(activityContext);
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String[] columns = {DbReaderHelper.COLUMN_CITYNAME,DbReaderHelper.COLUMN_ZIPCODE};
Cursor cur = db.query(DbReaderHelper.TABLE_NAME, columns,null,null,null,null,null);
cur.moveToFirst();
while(!cur.isAfterLast())
{
String city = cur.getString(cur.getColumnIndex(DbReaderHelper.COLUMN_CITYNAME));
String zipcode = cur.getString(cur.getColumnIndex(DbReaderHelper.COLUMN_ZIPCODE));
rowItems.add(new RowItem(city,Integer.parseInt(zipcode)));
cur.moveToNext();
}
cur.close(); // release all the resources
db.close();
mDbHelper.close();
Log.v(TAG, "loadinBackground() OUT");
return rowItems;
}
}
Here is the implementation of Loader Callbacks
public class ListFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<RowItem>>
{
private TextView emptyListTV;
private EditText zipcodeET;
private CustomAdapter customAdapter;
private List<RowItem> rowItems;
private ListView listView;
private final String TAG = "ListFragment";
private boolean fromRemoveItemFromListView = false; // TODO check this again
private final int LOADER_ID = 0x0;
public ListFragment()
{
// empty constructor
}
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG,"onCreate() IN");
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
Log.v(TAG,"onCreate() OUT");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_list, container, false);
initializeLayoutViews(view);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
getLoaderManager().initLoader(LOADER_ID,null,this).forceLoad();
}
@Override
public Loader<List<RowItem>> onCreateLoader(int id, Bundle args)
{
Toast.makeText(getActivity(),"onCreateLoader()",Toast.LENGTH_SHORT).show();
return new DataLoader(getActivity());
}
@Override
public void onLoadFinished(Loader<List<RowItem>> loader, List<RowItem> rowItems)
{
Toast.makeText(getActivity(),"onLoadFinished",Toast.LENGTH_SHORT).show();
customAdapter.setData(rowItems);
customAdapter.notifyDataSetChanged();
setMessageIfListEmpty();
}
@Override
public void onLoaderReset(Loader<List<RowItem>> loader)
{
Toast.makeText(getActivity(),"onLoaderReset()",Toast.LENGTH_SHORT).show();
customAdapter.setData(new ArrayList<RowItem>());
customAdapter.notifyDataSetChanged();
}
}
Here is my Logs when orientation changes
01-31 22:07:12.532 15019-15019/com.siddhant.myweatherapp D/AbsListView﹕ onDetachedFromWindow
01-31 22:07:12.583 15019-15019/com.siddhant.myweatherapp I/PersonaManager﹕ getPersonaService() name persona_policy
01-31 22:07:12.583 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onCreate() IN
01-31 22:07:12.653 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onStart() IN
01-31 22:07:12.733 15019-15019/com.siddhant.myweatherapp D/AbsListView﹕ Get MotionRecognitionManager
01-31 22:07:12.743 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() IN
01-31 22:07:12.743 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() OUT
01-31 22:07:12.743 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() IN
01-31 22:07:12.743 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() OUT
01-31 22:07:12.743 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onStart() OUT
01-31 22:07:12.753 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ OnResume() IN
01-31 22:07:12.753 15019-15522/com.siddhant.myweatherapp V/DataLoader﹕ loadinBackground() IN
01-31 22:07:12.753 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onResume() OUT
01-31 22:07:12.803 15019-15019/com.siddhant.myweatherapp E/ViewRootImpl﹕ sendUserActionEvent() mView == null
01-31 22:07:12.813 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onCreateOptionsMenu() IN
01-31 22:07:12.813 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onCreateOptionsMenu() OUT
01-31 22:07:12.953 15019-15522/com.siddhant.myweatherapp V/DataLoader﹕ loadinBackground() OUT
01-31 22:07:12.963 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() IN
01-31 22:07:12.963 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() OUT
Thanks,