如何从另一个活动刷新 asyncTask?

发布于 2024-11-29 15:28:04 字数 123 浏览 1 评论 0原文

我正在启动一个活动,一旦用户登录,我想刷新主要活动。从登录用户加载数据。

例如图像和名称。我已经把这一切都设置好了。

我只需要知道是否可以启动另一个活动并再次运行其异步任务。从另一个活动内部启动意图?

I am launching a activity, and once a user is logged in, i want to refresh the main activity. To load the data from the logged in user.

Such as the image and name. I have all of this set up already.

I just need to know is it possible to launch another activity and run its async task again.From an launching an intent from inside another activity?

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

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

发布评论

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

评论(3

污味仙女 2024-12-06 15:28:05

据我所知,AsyncTasks 不应该被重用。它们应该运行一次,然后您可以根据需要创建一个新的。

As far as I'm aware, AsyncTasks aren't supposed to be reused. They're supposed to be run once and then you can create a new one if you need it.

回首观望 2024-12-06 15:28:05

AsyncTask 一旦执行一次,就无法再次执行。不过,您可以做的是使用 onProgressUpdate()publishProgress() 控制它的“刷新”,如下所示。请注意,这仅适用于一次性刷新。如果您想在语义上更加正确,您可以在 onProgressUpdate() 中执行“正常”操作,并使用 onPostExecute() 进行刷新。

public class MyAsyncTask extends AsyncTask<Void, String, Void> {

    private boolean isRefresh = false;
    @Override
    protected Void doInBackground(Void... arg0) {
        while (!isRefresh){
            //Perform your normal operation
        }

    //When isRefresh is true, you want to refresh.  
        this.publishProgress(values);
        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        // Refresh code here
        super.onProgressUpdate(values);
    }

    public void refreshTask(){
        this.isRefresh = true;
    }

}

然后,您可以维护对 MyAsyncTask 对象的引用,并在需要刷新时调用 refreshTask()

Once an AsyncTask is executed once, you cannot execute it again. What you can do, though, is control it's "refresh" using onProgressUpdate() and publishProgress() as follows. Note that this will only work for a one-time refresh. If you wanted to be more semantically correct, you might do the "normal" operation in onProgressUpdate() and use onPostExecute() for your resfresh.

public class MyAsyncTask extends AsyncTask<Void, String, Void> {

    private boolean isRefresh = false;
    @Override
    protected Void doInBackground(Void... arg0) {
        while (!isRefresh){
            //Perform your normal operation
        }

    //When isRefresh is true, you want to refresh.  
        this.publishProgress(values);
        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        // Refresh code here
        super.onProgressUpdate(values);
    }

    public void refreshTask(){
        this.isRefresh = true;
    }

}

You could then maintain a reference to the object of MyAsyncTask and invoke refreshTask() on it whenever you want to refresh it.

2024-12-06 15:28:04

目前尚不清楚您的设计到底是什么,但如果您需要从两个不同的活动使用相同的 AsyncTask,它应该是一个单独的类,而不是绑定到特定的活动。您可以让这两个活动实现一个通用接口,以便 AsyncTask 不需要知道它正在更新哪个活动。然后通过传递对封闭活动的引用来实例化任务,并根据需要启动它。不需要一项活动来启动另一项活动。

像这样的东西:

public interface UserActivity {
  void updateUserData(UserData userData);
}

public class Activity1 implements UserActivity {

   public void onStart() {
      UpdateUserDataTask task = new UpdateUserDataTask(this);
      task.execute();
   }

   public void updateUserData(UserData userData) {
      // update
   }
}

public class UpdateUserDataTask extends AsyncTask<Void, Void, UserData> {
    UserActivity userActivity;

    public UpdateUserDataTask(UserActivitiy userActivity) {
       this.userActivity = userActivity;
    }

    // doInBackground, etc implementation.

    protected void onPostExecute(UserData userData) {
       userActivity.updateUserData(userData);
    }
}

It's not clear what exactly your design is, but if you need to use the same AsyncTask from two different activities, it should be a separate class, not tied to a particular activity. You can have the two activities implement a common interface, so that the AsyncTask doesn't need to know which activity it is updating. Then instantiate the task by passing a reference to the enclosing activity, and start it as needed. There is no need for one activity to start the other.

Something like:

public interface UserActivity {
  void updateUserData(UserData userData);
}

public class Activity1 implements UserActivity {

   public void onStart() {
      UpdateUserDataTask task = new UpdateUserDataTask(this);
      task.execute();
   }

   public void updateUserData(UserData userData) {
      // update
   }
}

public class UpdateUserDataTask extends AsyncTask<Void, Void, UserData> {
    UserActivity userActivity;

    public UpdateUserDataTask(UserActivitiy userActivity) {
       this.userActivity = userActivity;
    }

    // doInBackground, etc implementation.

    protected void onPostExecute(UserData userData) {
       userActivity.updateUserData(userData);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文