如何在展示前预加载一个片段?[英] How to pre-load a fragment before showing?

本文是小编为大家收集整理的关于如何在展示前预加载一个片段?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在我的活动中,我有几个全屏片段,每个片段都从Web下载一些数据(使用异步任务)并将其显示给用户.片段一次显示一个.

要更具体,每个片段读取了SQLite数据库中的一些URL,并在内容显示在列表中之前(如果很重要)之前获取内容.数据加载任务可以在ongreate()函数中完成.

当我显示飞溅屏幕时,我想预紧所有片段(至少开始下载).几乎就像是预贴的片段.

我想知道如何实现这一目标?我尝试初始化/创建我的活动的ongreate()函数中的所有片段,希望片段的otCreate()可以较早地调用,但是直到片段为片段为止,片段的onCreate()和oncreateview()函数才调用即将向用户展示.

推荐答案

听起来您需要将模型(已下载的数据)与视图(片段)分开.一种方法是在您的活动中启动下载异步性,而不是在每个片段中启动它们.然后,当最终显示片段时,他们可以显示已下载的数据(或旋转器或其他一些迹象,表明下载过程仍在执行).

其他推荐答案

碎片的肿瘤creative(捆绑包)告诉片段,其活动已经完成了自己的活动.

因此,您对此问题的解决方案是初始化或创建或做您要在片段创建片段之前要预紧的东西,在您的Fragment's onActivityCreated(Bundle)

请参阅 fragment lifecyle的文档

其他推荐答案

您可以开始加载的最早速度是在静态单例中或应用程序类

本文地址:https://www.itbaoku.cn/post/2091063.html

问题描述

In my activity I have several fullscreen fragments, each of them downloads some data from web (using an async task) and shows them to the user. The fragments are showed one at a time.

To be more specific, each of the fragment readings some urls from a sqlite database, and fetch the content before showing them in a list, if that matters. The data loading tasks can be done in the OnCreate() function.

I would like to preload all the fragment (at least starting the downloading), when I show a splash screen. Pretty much like a viewpager preload its fragments.

I am wondering how to achieve this? I tried initialize/create all the fragments in the OnCreate() function of my activity, hoping the OnCreate() of fragments could be called earlier, but the OnCreate() and OnCreateView() function of the fragments are not called until a fragment is about to show to the user.

推荐答案

It sounds like you need to separate your model (the data which is downloaded) from your view (the fragments). One way to do this is to start the downloading AsyncTasks in your activity, rather than starting them in each fragment. Then when the fragments are eventually displayed they can show the data which has been downloaded (or a spinner or some other indication that the download process is still executing).

其他推荐答案

Fragment's onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().

So your solution to this problem is initialize or create or do your stuffs which you want to preload before fragments are created, inside your Fragment's onActivityCreated(Bundle)

see documents for fragment's lifecyle

其他推荐答案

The earliest pace you can start loading is either in a static singleton or in the Application Class