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

查看:80
本文介绍了如何在显示之前预加载片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

更具体地说,每个片段都会从sqlite数据库中读取一些网址,并在将其显示在列表中之前获取内容(如果有的话).数据加载任务可以在OnCreate()函数中完成.

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.

当我显示启动画面时,我想预加载所有片段(至少开始下载).就像viewpager预加载其片段一样.

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.

我想知道如何实现这一目标?我尝试在活动的OnCreate()函数中初始化/创建所有片段,希望可以更早调用片段的OnCreate(),但是直到片段被调用时才调用片段的OnCreate()和OnCreateView()函数.即将显示给用户.

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.

推荐答案

听起来您需要从视图(片段)中分离模型(下载的数据).一种方法是在您的活动中开始下载AsyncTasks,而不是在每个片段中开始下载它们.然后,当最终显示片段时,它们可以显示已下载的数据(或微调框或某些其他指示,表明下载过程仍在执行中.)

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).

这篇关于如何在显示之前预加载片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆