Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop ASyncTask from crashing when my activity changes or destroys?

This MAY be a duplicate of another question, I am not sure. I've read similar questions, but either wasn't able to make sense of it, or wasn't able to successfully apply the given solutions.

I've created an application that has multiple tabs. Most tabs use an ASyncTask to update data when the users slides into the tab. However, when something about the activity changes while the AST is still running (that happens a lot; eg when the user rotates the screen or exits using the back button), the application will force close (ANR).

Here's what I see happening in my LogCat:

10-30 11:30:01.422: E/AndroidRuntime(4687): FATAL EXCEPTION: main
10-30 11:30:01.422: E/AndroidRuntime(4687): java.lang.NullPointerException
10-30 11:30:01.422: E/AndroidRuntime(4687):     at com.appconstructor.khcreates.Core$refreshList.onPostExecute(Core.java:913)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at com.appconstructor.khcreates.Core$refreshList.onPostExecute(Core.java:1)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at android.os.Looper.loop(Looper.java:137)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at java.lang.reflect.Method.invokeNative(Native Method)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at java.lang.reflect.Method.invoke(Method.java:511)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-30 11:30:01.422: E/AndroidRuntime(4687):     at dalvik.system.NativeStart.main(Native Method)

From what I can see, the ASyncTask (refreshlist) can't handle the fact that there's no application left to apply the change to.

What I want to do is make an application that doesn't force close if there's no activity.

  1. EITHER I want the ASyncTask to stop if the activity stops
  2. OR I want the application to just keep the task running, but not force close (eg: nothing happens onPostExecute() if there's no activity connected.

If someone needs my onPostExecute() code to solve the problem, let me know.

This has been bugging me for a while, and I haven't been able to solve this for myself. My app's experience is pretty horrible with this bug in place. Help would VERY much be appreciated!

Thanks in advance!

like image 250
Vic V Avatar asked Dec 10 '25 15:12

Vic V


2 Answers

Keep a reference to your task, and in the onDestroy and onConfigurationChanges methods, cancel the task..

if(task != null && task.getStatus() == Status.RUNNING)
    task.cancel(true);
like image 122
Nermeen Avatar answered Dec 12 '25 06:12

Nermeen


It's better to execute cancel(true) with same instance that you used for execute() command:

AsyncTask<Void,Void,Void> task = new MyAsyncTask();
task.execute();

.....

task.cancel(true);

EDIT:You need to store instance of your AsyncTask individualy in each fragment, so save instance of new freshList(this,getActivity(),mNum) as you fragment's variable and, since this is a fragment, execute cancel(true) in onPause() method of your fragments. Hope this will help you.

like image 38
Evos Avatar answered Dec 12 '25 06:12

Evos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!