AsyncTask 从 doInBackground 保存变量

发布于 2024-11-27 19:23:04 字数 391 浏览 0 评论 0原文

我的 Activity 中有两个 AsyncTasks 作为内部类。在 doInBackground 中返回一个 ArrayList,并在 postExecute 上为其分配一个 ListAdapter。另一个 AsyncTask 返回一个 StringArray 并设置一些 TextViews

旋转时一切都消失了,旋转时布局也发生了变化。

我想访问 doInBackground 方法的结果。如果我有访问权限,我可以简单地将变量保存在 onSaveInstanceState 中并手动重新分配值。

I have two AsyncTasks as inner classes in my Activity. One returns an ArrayList in doInBackground and asigns a ListAdapter to it on postExecute. The other AsyncTask returns a StringArray and sets some TextViews.

On Rotation everything is gone, also the layout changes on Rotation.

I'd like to have access to the results of the doInBackground-Methods. If I had access I could just simply save the variables in onSaveInstanceState and reasign the values manually.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不甘平庸 2024-12-04 19:23:04

您可以在onPostExecute 中访问doInBackground 的结果。

只需将您的班级更改为:

public class YourTask extends AsyncTask<Void, Void, ObjectYouWantToReturn> {

    @Override
    protected ObjectYouWantToReturn doInBackground(Beneficiary... params) {        
        ObjectYouWantToReturn obj = new ObjectYouWantToReturn();
        //... do your stuff
        return obj;
    }

    @Override
    protected void onPostExecute(ObjectYouWantToReturn result) {
        //there you go, here you have the results from doInBackground
    }
}

You can access the results of doInBackground in onPostExecute.

Simply change your class to:

public class YourTask extends AsyncTask<Void, Void, ObjectYouWantToReturn> {

    @Override
    protected ObjectYouWantToReturn doInBackground(Beneficiary... params) {        
        ObjectYouWantToReturn obj = new ObjectYouWantToReturn();
        //... do your stuff
        return obj;
    }

    @Override
    protected void onPostExecute(ObjectYouWantToReturn result) {
        //there you go, here you have the results from doInBackground
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文