[N.B. This problem is from an old app; developed couple of years ago. Back then the app was working fine]
Case 1:
I was trying to show some SQLite data inside a fragment. (Extending Listfragment and using viewPager).
The problem is the data was showing but not updating while the app was running. I tried adapter.notifyDataSetChanged();
It was not working either. Only When I relaunch the app, the updated data shows up.
Case 2:
Then I tried to use onCreate(savedInstanceState) while fragment is visible. Now when I navigate to that fragment from any other fragments, the container/viewpager gone invisible. But when I lock and unlock my device while staying on that fragment, the data shows up. But still data is not updated
Either way, I can not view updated data without relaunching app.
I am attaching the code which I tried,
MainActivity.Java
public class MainActivity extends BaseActivity implements ScannerFragmentList.OnScannerListFragmentInteractionListener {
private static final String TAG = MainActivity.class.getSimpleName();
private TabLayout tabLayout;
private ViewPager viewPager;
private ProgressDialog barcodeDownloadingProgressDialog;
private int[] tabIcons = { R.drawable.ic_camera, R.drawable.ic_history };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpToolBar();
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
if (PreferenceUtil.isStartInScanModeOn(getApplicationContext())){
onListFragmentInteraction(null,-1,0);
}
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new ScannerFragmentList(), getString(R.string.scan));
adapter.addFrag(new HistoryFragment(), getString(R.string.history));
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
Log.d(TAG,new Exception().getStackTrace()[0].getMethodName() + " " + position);
if (position == 0) {
return ScannerFragmentList.newInstance(1);
} else if (position == 1) {
return HistoryFragment.newInstance();
}
return null;
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
HistoryFragment.java (The problem is with this fragment)
public class HistoryFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int HISTORY_LOADER_ID = 100;
private static final String TAG = HistoryFragment.class.getSimpleName();
private HistoryListAdapter mAdapter;
private SQLiteCursorLoader cursorLoader;
Bundle save;
View view;
public static HistoryFragment newInstance(){
return new HistoryFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
save=savedInstanceState;
Log.d(TAG, new Exception().getStackTrace()[0].getMethodName());
getLoaderManager().initLoader(HISTORY_LOADER_ID, null, this);
mAdapter = new HistoryListAdapter(getContext(),null,true);
setListAdapter(mAdapter);
}
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser) {
onDestroy();
onCreate(save); //causing the issue in "case 2" mentioned above. If I remove it, data is showing but not updating until relaunch the app. I tried to put mAdapter.notifyDataSetChanged(); here. but did not work
}
}
private class HistoryListAdapter extends CursorAdapter {
private ActionMode mActionMode;
public HistoryListAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.history_list_item, null);
HistoryViewHolder historyViewHolder = new HistoryViewHolder();
historyViewHolder.mContentView = (TextView) view.findViewById(R.id.content_history);
historyViewHolder.mDateView = (TextView) view.findViewById(R.id.date_history);
historyViewHolder.mTimeView = (TextView) view.findViewById(R.id.time_history);
view.setTag(historyViewHolder);
return view;
}
@Override
public void bindView(final View view, Context context, final Cursor cursor) {
final HistoryViewHolder historyViewHolder = (HistoryViewHolder) view.getTag();
final long id = cursor.getLong(cursor.getColumnIndex(HistoryDatabaseHelper.COLUMN_ID));
String content = cursor.getString(cursor.getColumnIndex(HistoryDatabaseHelper.COLUMN_BARCODE_CONTENT));
long dateTime = cursor.getLong(cursor.getColumnIndex(HistoryDatabaseHelper.COLUMN_DATE));
historyViewHolder.mContentView.setText(HistoryUtil.getDisplayableContent(content));
historyViewHolder.mDateView.setText(DateFormat.getDateInstance().format(new Date(dateTime)));
historyViewHolder.mTimeView.setText(DateFormat.getTimeInstance().format(new Date(dateTime)));
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor c = HistoryDatabaseHelper.getInstance(getContext()).getHistoryById(id);
if (c.moveToFirst()) {
Intent intent = new Intent(getActivity(), BarcodeDetailsActivity.class);
intent.putExtra(Constant.ID, c.getLong(c.getColumnIndex(HistoryDatabaseHelper.COLUMN_ID)));
intent.putExtra(Constant.BARCODE_FORMAT, c.getString(c.getColumnIndex(HistoryDatabaseHelper.COLUMN_BARCODE_FORMAT)));
intent.putExtra(Constant.BARCODE_CONTENT, c.getString(c.getColumnIndex(HistoryDatabaseHelper.COLUMN_BARCODE_CONTENT)));
intent.putExtra(Constant.TIME_STAMP, c.getLong(c.getColumnIndex(HistoryDatabaseHelper.COLUMN_DATE)));
intent.putExtra(Constant.RAW_IMAGE_DATA, Util.hexStringToByteArray(c.getString(c.getColumnIndex(HistoryDatabaseHelper.COLUMN_RAW_IMAGE))));
startActivity(intent);
}
}
});
}
}
private static class HistoryViewHolder{
TextView mContentView;
TextView mDateView;
TextView mTimeView;
}
@NonNull
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.d(TAG,new Exception().getStackTrace()[0].getMethodName());
String sql = "SELECT * FROM " + HistoryDatabaseHelper.TABLE_NAME;
cursorLoader = new SQLiteCursorLoader(getContext(),HistoryDatabaseHelper.getInstance(getContext()),sql,null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.d(TAG,new Exception().getStackTrace()[0].getMethodName());
mAdapter.changeCursor(data);
mAdapter.notifyDataSetChanged(); //onCreate(save) force this fragment to call onLoadFinished(). So I put mAdapter.notifyDataSetChanged(); here
}
@Override
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
Log.d(TAG,new Exception().getStackTrace()[0].getMethodName());
mAdapter.changeCursor(null);
}
@Override
public void onDestroy() {
Log.d(TAG,new Exception().getStackTrace()[0].getMethodName());
super.onDestroy();
}
}
I tried so many things so far, but none of them seems to work. I want to know, what is the problem here and How can I solve this issue?