0

我有三页的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仍在运行。所以我想要的是,当改变方向并且Dialog被关闭时,再次显示它直到AsyncTask结束。

4

3 回答 3

1

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

安卓3.0以上

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

安卓3.0以下

<activity
            android:name="yourActivityWhichContainsViewPager"
            android:configChanges="orientation"
            />
于 2013-09-02T11:39:54.027 回答
1

根据我的说法,您dismiss()的对话in onPause()可能是您的活动正在重新启动,但您的AsyncTask.

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

于 2013-09-02T11:46:51.143 回答
0

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

<activity android:name="Your Activity Name"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="adjustPan"/>
于 2013-09-02T11:39:16.807 回答