在ViewPager中的AsyncTask中更改方向时,关闭并显示对话框

时间:2013-09-02 11:34:41

标签: android dialog android-viewpager progressdialog

我有三页的 ViewPager 。在其中一个中,我在 AsyncTask 中显示 ProgressDialog

问题是指在更改方向时,对话框将被解除并且不会显示。

这是我的代码:

的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;
    }
}

正如我所说,在更改方向, ProgressDialog 消失,但 AsyncTask 仍在运行。所以我想要的是,当更改方向和对话框被取消时,再次显示它,直到 AsyncTask 结束。

3 个答案:

答案 0 :(得分:1)

这是因为每当屏幕方向改变时,活动都会重新加载。您可以通过manifest.xml文件中的单行解决此问题

高于Android 3.0

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

Android 3.0以下

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

答案 1 :(得分:1)

根据我的说法dismiss()您的对话in onPause(),因此可能会出现您的活动重新启动而不是AsyncTask的情况。

如果可能,请正确粘贴您的代码。

答案 2 :(得分:0)

在你的清单中试试这个......

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