0

I have ViewPager with three pages. In one of them I show a ProgressDialog in an AsyncTask.

Problem is that when change orientation, Dialog is dismissed and doesn't appear.

Here is my code:

AndroidManifest.xml

<activity 
    android:name=".LecturaFragment"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
    android:windowSoftInputMode="adjustPan"  >
</activity>

SherlockFragmentActivity

lecturaFragment.new LoadingLecturaTask(this).execute(lecturas);

//In LecturaFragment (SherlockFragment)
private ProgressDialog dialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);    
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    ...
}

public void onPause() {
    super.onPause();

    if(dialog != null) {
        dialog.dismiss();
    }
    dialog = null;
}

public class LoadingLecturaTask extends AsyncTask<String, Void, Boolean> {
    private Activity activity;
    private volatile boolean running = true;
    private int lecturas;
    private String result = "";
    /** application context. */
    private Context context;

    public LoadingLecturaTask(Activity activity) {
        this.activity = activity;
        context = activity;
        dialog = new ProgressDialog(context);
        dialog.setCancelable(true);
        dialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                cancel(true);
            }
        });
    }

    @Override
    protected void onCancelled() {
        running = false;
    }        

    @Override
    protected void onPreExecute() {
        dialog.setMessage(getString(R.string.loading_envio));
        dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog != null && dialog.isShowing()) {
        dialog.dismiss();
        }

        if (success) {
            ...
        }
    }

    @Override
    protected Boolean doInBackground(final String... args) {
        lecturas = Integer.parseInt(args[0]);
        while (running) {
            try{
                ...                 
                return true;
            } catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
        return null;
    }
}

As I said, in change orientation, ProgressDialog disappears, but AsyncTask is still running. So what I want is, when change orientation and Dialog is dismissed, show it again until AsyncTask ends.

CSchulz
  • 10,102
  • 9
  • 54
  • 107
Lyd
  • 2,046
  • 3
  • 24
  • 33

3 Answers3

1

It happens because whenever screen orientation changes activity is reload. You can solve this issue by single line in manifest.xml file

Above Android 3.0

<activity
            android:name="yourActivityWhichContainsViewPager"
            android:configChanges="keyboardHidden|orientation|screenSize"
            />

Below Android 3.0

<activity
            android:name="yourActivityWhichContainsViewPager"
            android:configChanges="orientation"
            />
Biraj Zalavadia
  • 27,124
  • 9
  • 56
  • 73
  • Wow! I've found the problem...I had it in `LecturaFragment`but not in `Activity`that contains `ViewPager`. Thanks! – Lyd Sep 02 '13 at 11:44
1

According to me your you dismiss() your dialog in onPause() so there may be a case that your activity is getting restarted but not your AsyncTask.

If possible please paste your code properly.

Siddhesh
  • 1,320
  • 11
  • 28
0

Try this in your manifest...

<activity android:name="Your Activity Name"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="adjustPan"/>
Hariharan
  • 28,002
  • 7
  • 50
  • 55
  • I forgot about that...I actually have it in my AndroidManifest (added right now in my question) – Lyd Sep 02 '13 at 11:41