在嵌套片段中运行多个AsyncTasks时挂起线程,这会使应用程序变慢

时间:2016-01-28 06:39:38

标签: android multithreading user-interface android-fragments android-asynctask

这里我使用TabLayoutViewPagerAsyncTask嵌套片段中调用Web服务。我已经尝试AsyncTasks通过在每个onResume的{​​{1}}方法中将其全部调用为:{/ 1>

Fragment

new FetchAllData(getActivity()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

一切都很好,但我遇到了很多问题:

  • 应用程序在没有任何对话框的情况下停止工作,但它在logcat中显示new FetchAllData(getActivity()).execute();

  • 当我们从图像中显示的另一个活动中获取此片段(具有嵌套的tablayouts和viewpagers)时,除了工具栏之外,所有内容都会变为空白3到5秒。突然之间,他们带来了完整的数据。

  • 当我们从同一活动的另一个片段调用这个片段时,它会冻结在那里,这些片段会在那里突然打开。

我希望你能解决我的所有问题,请告知我。

Stacktrace是:

suspending all threads

Fragment.java

1-29 12:10:49.580 10853-10863/com.cws.advisorymandi W/art: Suspending all threads took: 10.409ms
 01-29 12:10:49.707 10853-10853/com.cws.advisorymandi D/cr_Ime: [InputMethodManagerWrapper.java:27] Constructor
 01-29 12:10:49.711 10853-10853/com.cws.advisorymandi D/cr_Ime: [ImeAdapter.java:241] attach
 01-29 12:10:49.711 10853-10853/com.cws.advisorymandi W/art: Attempt to remove local handle scope entry from IRT, ignoring
 01-29 12:10:49.716 10853-10853/com.cws.advisorymandi W/AwContents: onDetachedFromWindow called when already detached. Ignoring
 01-29 12:10:49.717 10853-10853/com.cws.advisorymandi D/cr_Ime: [InputMethodManagerWrapper.java:56] isActive: false
 01-29 12:10:49.726 10853-10853/com.cws.advisorymandi W/art: Attempt to remove local handle scope entry from IRT, ignoring
 01-29 12:10:49.726 10853-10853/com.cws.advisorymandi W/art: Attempt to remove local handle scope entry from IRT, ignoring
 01-29 12:10:49.812 10853-10853/com.cws.advisorymandi D/cr_Ime: [ImeAdapter.java:241] attach
 01-29 12:10:49.832 10853-10853/com.cws.advisorymandi D/cr_Ime: [ImeAdapter.java:241] attach
 01-29 12:10:49.833 10853-10853/com.cws.advisorymandi I/Choreographer: Skipped 58 frames!  The application may be doing too much work on its main thread.
 01-29 12:10:50.075 10853-10853/com.cws.advisorymandi W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 10853
 01-29 12:10:50.610 10853-10863/com.cws.advisorymandi W/art: Suspending all threads took: 43.636ms
 01-29 12:10:50.621 10853-10853/com.cws.advisorymandi I/Ads: Scheduling ad refresh 60000 milliseconds from now.
 01-29 12:10:50.630 10853-10853/com.cws.advisorymandi I/Ads: Ad finished loading.

有关完整的堆栈跟踪,请转到http://pastebin.com/7FDynA05

3 个答案:

答案 0 :(得分:2)

我知道我已经很晚了但是当我看到你对应用程序处理的很多问题时我发现没有任何错误,但我仍然想说请检查你是否正在调用TypeFace通过每次创建一个新对象来评论它们并运行你的代码并让我知道。

我很久以前也遇到过这类问题因为TypeFace所以请试试我的回答让我知道。

感谢。

答案 1 :(得分:1)

首先我建议你使用任何网络库都可以截击或改造。因为它们更有效,并且它们将在后台和并行处理呼叫而不使用AsyncTask。

你正在尝试的方式是最复杂的,你同时打电话也是如此。

如果需要,则仅在恢复时进行网络呼叫。您可以在onCreateView中调用它。或者您甚至可以选择在开始时调用它。

答案 2 :(得分:0)

不要在活动或片段中执行操作,或者每次修改用户界面时都必须管理问题(同时也使用异步任务)。

您可以使用asynch任务或库,例如volley,但可以使用服务。一个好的教程是here