[android-developers] How to dismiss nested dialogfragment correctly after rotation?
I use appcompat library and support library v4 to host some fragments for tabs. In a specific fragment A, I need to do network operation. In order not to block the UI thread, I execute an AsyncTask in fragment A and do the network operation in doInBackGround() of the AsyncTask.
To prevent messing around keep AsyncTask to survive rotation in the fragment, I call setRetainInstance(true) in the fragment A oncreate().
It works fine but I need to prompt user something when the network operation is in progress. Therefore, I use DialogFragment. In the DialogFragment onCreateView(), it instantiates a ProgressDialog and returns.
To be concrete, the following is the FragmentA, MyDialogFragment and AsyncTask code snippets:
private boolean isNetworkOperationCalled;
private MyDialogFragment myDialogFragment;
...
public void showProgressDialog(){ //for MyAsyncTask#onPreExecute()
myDialogFragment = new MyDialogFragment();
myDialogFragment.show(getFragmentManager(), "mydialog");
}
public void showProgressDialog(){ //for MyAsyncTask#onPostExecute()
myDialogFragment.dismiss();
}
public void onResume() {
if(!isNetworkOperationCalled){
new MyAsyncTask.execute();//do the network operation
}
}
...
}
public class MyDialogFragment extends DialogFragment{ //ProgressDialog for FragmentA
...
public Dialog onCreateDialog(Bundle savedInstanceState) {
ProgressDialog progressDialog = new ProgressDialog(getActivity(),ProgressDialog.STYLE_SPINNER);
... //set the dialog attributes
return progressDialog;
}
...
}
public class MyAsyncTask extends AsyncTask<Void,Void,Void>{ //do network operation for fragment A
private Fragment fragment;
fragment = fragmentA;
}
protected void onPreExecute() {
fragment.showProgressDialog(); //show progess dialog for users
}
protected String doInBackground(Void... arg0) {
...
}
protected void onPostExecute() { //dismiss the dialog
fragment.dismissProgressDialog();
}
}
The code (method) above works if there is no rotataion occurs while doInBackground() is executing. Nevertheless, if the rotation happens before onPostExecute() is called,
the rotation is fine and the progress dialog is retained. Sadly, it crashes when onPostExecute() finally be called to dismiss the dialog.
E/AndroidRuntime(27493): FATAL EXCEPTION: main
E/AndroidRuntime(27493): java.lang.NullPointerException
E/AndroidRuntime(27493): at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:184)
E/AndroidRuntime(27493): at android.support.v4.app.DialogFragment.dismiss(DialogFragment.java:155)
E/AndroidRuntime(27493): at cyl.ccu.cia.FragmentA.dismissProgressDialog(FragmentA.java:205)
E/AndroidRuntime(27493): at cyl.ccu.cia.FragmentA.access$1(FragmentA.java:203)
E/AndroidRuntime(27493): at cyl.ccu.cia.FragmentA$GetTokenAsyncTask.onPostExecute(FragmentA.java:295)
E/AndroidRuntime(27493): at cyl.ccu.cia.FragmentA$GetTokenAsyncTask.onPostExecute(FragmentA.java:1)
E/AndroidRuntime(27493): at android.os.AsyncTask.finish(AsyncTask.java:631)
E/AndroidRuntime(27493): at android.os.AsyncTask.access$600(AsyncTask.java:177)
E/AndroidRuntime(27493): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
E/AndroidRuntime(27493): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(27493): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(27493): at android.app.ActivityThread.main(ActivityThread.java:4875)
E/AndroidRuntime(27493): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(27493): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(27493): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:804)
E/AndroidRuntime(27493): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:571)
E/AndroidRuntime(27493): at dalvik.system.NativeStart.main(Native Method)
How can I solve the problem so that the dialog can dismiss correctly after rotation?
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups "Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home