In my main action, I have a fragment in which I use setRetainInstance (true), so the AsyncTask used in it is not violated by a change of orientation.
A lot of work is handled by AsyncTask. Therefore, I would like to display a dialog with progressBar on top of my activity.
I did some research and managed to do it using 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 do not change the rotation of the screen, it works fine. But if I do this, the dialog will be fired, and after a few seconds I get a NullPointerEsception, which is pointless since I set the condition: if (dialog! = Null)
What am I doing wrong?
source share