显示之前在后台加载活动 [英] Load an activity in the background before displaying it

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

问题描述

是否可以认为切换到该活动之前在后台加载新的活动?

Is it possible to load a new activity in the background before switching the view to that activity?

例如,我想有一个被调用并显示启动画面斜线屏幕活动。当显示这个闪屏,下一个活动被加载,当它完成加载(当它的onCreate()完成),则启动画面活动结束,并显示新的活动。

For example, I would like to have a slash screen activity that gets called and displays a splash screen. While this splash screen is displayed, the next activity is loaded, and when it is done loading (when it's onCreate() is finished) then the splash screen activity ends, and the new activity is displayed.

我知道另一种选择是在新的活动来显示启动画面,并使用异步任务删​​除开机画面前装入所有数据...但我被困在这种方法为好。活动首先必须加载数据的相当数量的,然后它具有动态地添加基于该数据的GUI元素。一旦GUI满载,然后我想删除的启动画面。问题是,我无法从doInBackground触摸UI线程()。如何创建一个我闪屏后面的活动,如果我不能更新从doInBackground的用户界面?我知道,onProgressUpdate()可以访问UI线程,但我不能想出如何实现它。

I know another option would be to display the splash screen in the new activity, and use async task to load all the data before removing the splash image... but I am stuck on that approach as well. The activity first has to load a fair amount of data, and then it has to dynamically add GUI elements based on that data. Once the GUI is fully loaded, I then want to remove the splash screen. The problem is that I cannot touch the UI thread from doInBackground(). How do I create my activity behind a splash screen, if I cannot update the UI from doInBackground? I know that onProgressUpdate() can access the UI thread, but I can't figure out how to implement it.

任何想法?谢谢!

推荐答案

既然你没有你的code的例子,我不知道你是装什么样的数据,以及你是如何动态配置UI基础上的数据,但我会尽力回答,因为尽我所能。其结果是,答案可能听起来有点普通。

Since you don't have an example of your code, I am not sure what kind of data you are loading and how you are dynamically configuring the UI based on the data, but I'll try to answer as much as I can. As a result, the answer may sound a little generic.

首先,定义2布局的XML文件 - 一个用于闪屏的,一个是主的活动。
所以,你会/res/layout/splash_screen.xml和/res/layout/main.xml结束

First, define 2 layout xml files - one for the splash screen and one for your "main" activity. So you'll end up with /res/layout/splash_screen.xml and /res/layout/main.xml

在你的onCreate(),加载splash_screen布局:

In your onCreate(), load the splash_screen layout:

setContentView(R.layout.splash_screen);

在你的异步任务中,您将加载你需要做的任何数据,你会保存一些数据结构的所有数据。我会用String的链表为例的缘故。

In your async task, you will load up whatever data you need to do, and you will save all that data in some sort of data structure. I'm gonna use a LinkedList of String for example's sake.

private class MyTask extends AsyncTask<Uri, Integer, List<String>> {

    @Override
    protected List<String> doInBackground(Uri... params) {
        List<String> myList = new LinkedList<String>();
        // load up the list with data you are trying to get
        myList.add("foo");
        myList.add("bar");
        // whatever you return here will be passed in as a parameter to the onPostExecute()
        return myList;
    }

    @Override
    protected void onPostExecute(List<String> result) {
        setContentView(R.layout.main2);
        // set layout elements with data that from the result
        TextView myTextView = (TextView) findViewById(R.id.some_label);
        myTextView.setText(result.get(0));
        // or just call some function you defined in your activity instead
    }
}

所以基本上,有2个不同的布局文件,并使用splash_screen布局,并使用异步任务负载数据并将其保存在你定义一些数据结构,并使用该数据结构来加载onPostExecute你的UI元素()使用的setContentView()后改回主布局。

So basically, have 2 different layout file and use the splash_screen layout, and use the async task the load the data and save it in some data structure you define, and use that data structure to load your UI elements in onPostExecute() after using setContentView() to change back to your main layout.

一个特别说明:
有了上面code,它会再次出现闪屏,如果你旋转屏幕重新加载的所有数据。如果你想避免这种情况,可以使用的onSaveInstanceState(),并保存你想要的任何数据在outBundle并读取该数据在回onCreate事件savedInstanceState包和加载UI元素回升。这将需要一个单独的线程(或者你可以搜索一下),如果你想知道更多有关处理旋转。

One special note: With the above code, it will show the splash screen again and reload all the data again if you rotate the screen. If you want to avoid that, you can use the onSaveInstanceState() and save whatever data you want in the outBundle and read that data back in onCreate's savedInstanceState bundle and load the UI elements back up. This will require a separate thread (or you can just search about it) if you wanted to know more about handling rotation.

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

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