Android 异步任务和

发布于 2024-09-30 19:09:02 字数 419 浏览 0 评论 0原文

您好,我的应用程序中有一个名为 OnCreate() 的 AsyncTask,它通过网络检索一些数据并在下载时显示不确定的进度条。

问题是当我启动应用程序时,屏幕保持空白,直到 AsyncTask 完成。 代码就是这样的。

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    loadData();
    //Several UI Code   
    startAsyncTasks();
}


 private void startAsyncTasks(){
    new ConnectingTask().execute();
 }

Hi i have an AsyncTask in my application called in OnCreate() that retrieve some data over the web and display an indeterminate progress bar while downloading.

The problem is when i start the app the screen remain blank until the AsyncTask is finished.
The code is something like that.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    loadData();
    //Several UI Code   
    startAsyncTasks();
}


 private void startAsyncTasks(){
    new ConnectingTask().execute();
 }

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

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

发布评论

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

评论(1

时光倒影 2024-10-07 19:09:02

OnCreate() 在 Activity 显示在屏幕上之前执行,因此整个数据下载过程在显示 Activity 之前执行。

解决方案可能是在 OnStart() 方法中启动 AsyncTask,而不是在 OnCreate() 中(也被覆盖)。 OnStart() 在 Activity 将要显示在屏幕上时执行(请参阅 Activity 生命周期)。

这是我在我的应用程序中实现的情况并且它有效。您应该在 OnStart() 方法中显示进度对话框,并在适​​当的时候在 AsyncTask 中更新和关闭它。

这就是它在我的应用程序中的样子:

  1. AsyncTask 在活动 OnStart() 中启动,并显示对话框
  2. 正在扩展 AsyncTask 的内部类中下载数据
  3. 下载数据后,对话框将关闭并初始化进一步的数据处理 (OnPostExecute())

缺点可能是 OnStart() 在第一次活动调用时被调用,而且在恢复它之后(例如最小化应用程序后)也会被调用。因此,AsyncTask 可以执行几次,与 OnCreate() 相反,OnCreate() 仅在创建 Activity 时(而不是恢复 Activity 时)调用一次。

如果您使用 AsyncTask 中的对话框对此类活动进行 jUnit 测试,也可能会出现一些问题 - 如果您这样做,请告诉我 - 将发布一些解决方案

OnCreate() is executed before the Activity is being shown on the screen, so the whole data download process executes before showing the activity.

Solution could be to start AsyncTask in OnStart() method instead of in OnCreate() (also overriden). OnStart() is executed while activity is going to be shown on the screen (see activity lifecycle).

This is the case I have implemented in my app and it works. You should show a progress dialog in OnStart() method, and update and dismiss it in AsyncTask in proper moments.

This is how it looks in my app:

  1. AsyncTask is started in activity OnStart() along with showing dialog box
  2. Data is being downloaded in inner class extending AsyncTask
  3. After data is downloaded the dialog box is dismissed and further data processing initialized (OnPostExecute())

The drawback could be that OnStart() is called on the first activity call, but also after restoring it (for example after minimizing the application). So AsyncTask can be executed few times in opposite to OnCreate(), that is called only once - when activity is created (not when it is restored).

There can also be some issues if You do the jUnit test of such activity with dialog in AsyncTask - let me know if You do - will post some solution

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