Android Tip: When AsyncTask goes wrong

Android Tip: When AsyncTask goes wrong

Every Android developer worth his title knows this problem: you have some long running logic you do not want to run on the main thread, so you offload it to an AsyncTask. The AsyncTask is doing its background work and then, when completed it updates the calling activity.

But hey, the activity might no longer there. It might have been pushed to the background and reclaimed by Android. It might have been recreated due to screen orientation switch. The reason is not important: if you try to update a non-existing activity bad things (read: NullPointerException) will follow.

Now, there are several ways of dealing with this problem, and only one correct way, sometimes called the "weak reference pattern". Let's go over it:

  1. Before your AsyncTask starts running, keep a weak reference to the calling activity in an AsyncTask member.
  2. Do your background logic within doInBackground() function
  3. When its time to update the UI (function onPostExecute) starts by checking the activity weak reference and, if it is null, immediately bail out. Your activity is no longer in existence.


Putting it into code we get:

 

And no more NullPointerException on user switching the screen.

The WeakReference is of extreme importance here, as it allows the owning activity to be  garbage collected by Android. NEVER use this pattern with a "strong" reference to an activity.

Pankaj Arora

Fintech | Payments | Spring-Boot | Backend | Rest | AWS | CI-CD | Docker

8 å¹´

Great article, you can add many more thing related to wrong implementation of async task.

赞
回复

要查看或添加评论,请登录

Gilad Haimov的更多文章

  • Training Model Reuse: Transfer Learning and PEFT

    Training Model Reuse: Transfer Learning and PEFT

    Transfer learning Transfer learning is a machine learning technique where a model trained on one task is reused as the…

    1 条评论
  • Advanced Solidity: Avoid Relying On Contract Balance

    Advanced Solidity: Avoid Relying On Contract Balance

    Why having your smart contract flow rely on its balance is a severe security breach A simple bidding contract Let's…

    1 条评论
  • Why Is Ethereum ERC-721 a Big Thing?

    Why Is Ethereum ERC-721 a Big Thing?

    The short answer: Because it allows you to buy and sale unique assets - assets with their own identity - via the…

    3 条评论
  • The 1-Hour?ICO

    The 1-Hour?ICO

    Creating and deploying a full fledge ERC20 token in less than an hour In the last couple of years the ERC20 token…

    1 条评论
  • ???????? ?? ??????: ????? ??? ??? – ??? ?

    ???????? ?? ??????: ????? ??? ??? – ??? ?

    ????? ?????? ?? ?????, ?????? ????? ????, ????? ?? ?????????? ?????? ?? ?????????? ???????? ?? ?????? ?????????? ?????…

  • ???????? ?? ??????: ????? ??? ??? – ??? ?

    ???????? ?? ??????: ????? ??? ??? – ??? ?

    ???????? ?? ?????? ??? ??? ??????? ??????? ????? ????????? ?? ?????. ????????? ??????? ???????? ??????, ????? ??????…

  • Android DDMS: A Guide to the Ultimate Android Console -- Part III

    Android DDMS: A Guide to the Ultimate Android Console -- Part III

    Device State Emulation in Android DDMS As a rule, mobile apps are not linear constructs. Instead, they deploy awareness…

  • Android DDMS: A Guide to the Ultimate Android Console -- Part II

    Android DDMS: A Guide to the Ultimate Android Console -- Part II

    Thread Info Console: Android CPU Usage Made Easy Well known to any developer, synchronous paths of executing logic are…

  • Android DDMS: A Guide to the Ultimate Android Console -- Part I

    Android DDMS: A Guide to the Ultimate Android Console -- Part I

    (previously published in Toptal's developer blog) Developing is a tricky business. The target keeps moving, new…

  • ?????? ?????? ???? ????????

    ?????? ?????? ???? ????????

    ??????? ?????? ????? ????? ???? ??????? ? ?????? ???????? ?? ?????? ?????? ???? ?????????. ?????? ????? ??? ???? ???…

社区洞察

其他会员也浏览了