DialogFragment on Fragment with screen rotation

android, fragment, rotation

Solution

Solution found!

I was not doing the right thing with the Fragment containing my AsyncTask.

Because, I haven't really understood the concept of orientation in Fragment, I get it thanks to this link: http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

Problem

On my main activity I have a Fragment in which I apply setRetainInstance(true) so that the AsyncTask I use into it is not disturbed by orientation change. A lot of work is processed by the AsyncTask. That's why I would like to display a dialog with a progressBar on top of my activity. I made some researches and I succeed in doing with a DialogFragment: public class DialogWait extends DialogFragment { ``` private ProgressBar progressBar; public DialogWait() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.dialog_wait, container); Dialog dialog = getDialog(); dialog.setTitle("Hello"); setCancelable(false); progressBar = (ProgressBar) view.findViewById(R.id.progress); return view; } public void updateProgress(int value) { progressBar.setProgress(value); } ``` And here is my AsyncTask: ``` public class InitAsyncTask extends AsyncTask<Void, Integer, Void> { private Context activity; private OnTaskDoneListener mCallback; private DialogWait dialog; public InitAsyncTask(Context context, OnTaskDoneListener callback, DialogWait dialogWait) { activity = context; mCallback = callback; dialog = dialogWait; } @Override protected Void doInBackground(Void... params) { doStuff(); return null; } @Override protected void onProgressUpdate(Integer... values) { dialog.updateProgress(values[0]); } @Override protected void onPostExecute(Void result) { publishProgress(100); if(dialog != null) dialog.dismiss(); mCallback.onTaskDone(); } private void doStuff() { //... } ``` } If I don't change the screen rotation, it works fine. But if I do, the dialog is dismissed and a few seconds later, I got a NullPointerEsception which nonsense since I set the condition: if(dialog != null) What am I doing wrong?

Original source