0

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();
}
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
MattMatt
  • 905
  • 6
  • 19
  • Each item in adapter would be an Fragment right? – Dinash Oct 11 '14 at 03:44
  • Correct, all of them are Fragments – MattMatt Oct 11 '14 at 03:44
  • Then just start progress dialog in onresume of each fragment and dismiss it as soon as you set the text. And don't use any thread.sleep which will always cause ANR – Dinash Oct 11 '14 at 03:47
  • 1
    I would like the Dialog to be in FragmentActivity rather than showing in a Fragment that may not be visible to the user. Any other suggestions? – MattMatt Oct 11 '14 at 03:51
  • Are you using UI thread? If you do, the 'sleep' will just block the system an load will never succeed. Move the code to a separate thread. – cyanide Oct 11 '14 at 05:56
  • Yes, I realized that I am indeed running on UI thread. I should've caught that. Should I use `Handler handler = new Handler(); handler.post(new Runnable{ // Dialog here});` – MattMatt Oct 12 '14 at 23:16

0 Answers0