Dialog fragment causing nullpointer to crash

In my application, I have several AsyncTasks that perform HTTP requests. If the task fails, I show the user the Dialog dialog box.

ErrorDialogFragment newFragment = ErrorDialogFragment.newInstance(R.string.dialog_message_error);
newFragment.show(getFragmentManager(), ConstantHandler.DIALOG_ERROR);

The problem is that the user sometimes shuts down the application or before the AsyncTask procedure completes, causing a null pointer exception in getFragmentManager.

My question is, is there a way to avoid this other than tracking if all fragments and activity are still running?

+2
source share
1 answer

I had the same problems. The good answer I saw on a blog somewhere (I forgot) is as follows:

AsyncTasks . /, AsyncTasks onRetainNonConfigurationInstance() getLastNonConfigurationInstance(). , , onCreate() , AsyncTask, getLastNonConfigurationInstance(). , . AsyncTask .

, , , null AsyncTasks, onRetainNonConfigurationInstance(). AsyncTask , Context null, - . , AsyncTask .

+3

All Articles