简体   繁体   English

使用片段的ViewPager

[英]ViewPager using Fragment

I am using ViewPager which uses three Fragments, and one of the Fragments uses database operations. 我正在使用使用三个片段的ViewPager,并且其中一个片段使用数据库操作。 So when I launch my application it takes much time to load(because of databse operation) and it crashes most of the time. 因此,当我启动我的应用程序时,它需要花费很多时间来加载(由于使用了databse操作),并且大多数时候它都崩溃了。 I used AsyncTask for databse operations in Fragment but still this problem is there. 我将AsyncTask用于Fragment中的数据库操作,但仍然存在此问题。 Any other solutions ?? 还有其他解决方案吗?

Have you already considered these rules for AsyncTasks? 您是否已经考虑过AsyncTasks的这些规则?

There are a few threading rules that must be followed for this class to work properly: 为了使此类正常工作,必须遵循一些线程规则:

  • The AsyncTask class must be loaded on the UI thread. 必须在UI线程上加载AsyncTask类。 This is done automatically as of JELLY_BEAN. 从JELLY_BEAN开始,这是自动完成的。
  • The task instance must be created on the UI thread. 必须在UI线程上创建任务实例。 execute(Params...) must be invoked on the UI thread. 必须在UI线程上调用execute(Params ...)。
  • Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually. 不要手动调用onPreExecute(),onPostExecute(Result),doInBackground(Params ...),onProgressUpdate(Progress ...)。
  • The task can be executed only once (an exception will be thrown if a second execution is attempted.) 该任务只能执行一次(如果尝试第二次执行,则会引发异常。)

from: http://developer.android.com/reference/android/os/AsyncTask.html 来自: http : //developer.android.com/reference/android/os/AsyncTask.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM