Android AsyncTask context behavior

前端 未结 4 2031
轮回少年
轮回少年 2020-12-23 17:11

I\'ve been working with AsyncTasks in Android and I am dealing with an issue.

Take a simple example, an Activity with one AsyncTask. The task on the background does

4条回答
  •  星月不相逢
    2020-12-23 17:28

    To avoid this you can use the answer givin here: https://stackoverflow.com/a/2124731/327011

    But if you need to destroy the activity (different layouts for portrait and landscape) you can make the AsyncTask a public class (Read here why it shouldn't be private Android: AsyncTask recommendations: private class or public class?) and then create a method setActivity to set the reference to the current activity whenever it is destroyed/created.

    You can see an example here: Android AsyncTask in external class

提交回复
热议问题