I have a ViewPager
containing 6 Fragments
in my app. Each fragment displays only text information to the user. I have given the user the option of saving/sharing this information, but want to make sure all of the fragments are loaded before the information can be shared.
The problem is when the information is not loaded yet, obviously a NullPointerException
, simply because it has not been defined yet. So I want a ProgressDialog
to pop up until the fragments are loaded.
I have mViewPager.setOffScreenLimit()
already set, and a dialog is no problem, but how to know when all data is loaded.
Here is the method I was working on, but it results in ANR, app not responding. Any and all help is appreciated, thank you for reading!
private void checkIfAllLoaded() {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Please wait");
dialog.setMessage("Please wait while the information is prepared...");
dialog.setCancelable(false);
dialog.setIndeterminate(true);
dialog.show();
for (int i = 0; i < mAdapter.getCount(); i++) {
if (!mAdapter.getItem(i).isAdded()) {
while (!mAdapter.getItem(i).isAdded()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
dialog.dismiss();
}