我有三页的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结束。