如何preLOAD的活动? [英] How to preload an Activity?

查看:142
本文介绍了如何preLOAD的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处去寻找这一点,但没有人似乎有一个答案。

我的简单的问题是:

有没有一种方法,以preLOAD活动?我需要这个,因为我用一个标签,一个标签有多个活动。我的一个活动是一个RSS阅读器,它加载pretty的硬盘(约2-3秒)。

所有我在网上找到一个joke.Everyone都有自己的观点,但没有人能指出你的样品code。等待一个答案,谢谢!

这是在code加载的饲料:

 在的onCreate:
//去让我们的饲料!
        饲料= getFeed(RSSFEEDOFCHOICE);

        //显示UI
        UpdateDisplay();

        倒计时();


和功能:

私人的RSSFeed getFeed(字符串urlToRssFeed)
    {
        尝试
        {
            //设置URL
           网址URL =新的URL(urlToRssFeed);

           //创建工厂
           的SAXParserFactory厂= SAXParserFactory.newInstance();
           //创建一个解析器
           的SAXParser解析器= factory.newSAXParser();

           //创建读取(扫描仪)
           的XMLReader的XMLReader = parser.getXMLReader();
           //实例化处理
           位于RSSHandler theRssHandler =新位于RSSHandler();
           //指定我们的处理
           xmlreader.setContentHandler(theRssHandler);
           //通过URL类让我们的数据
           InputSource的是=新的InputSource(url.openStream());
           //执行同步解析
           xmlreader.parse(是);
           //得到的结果 - 应该是一个完全填充的RSSFeed的实例,或在错误空

           返回theRssHandler.getFeed();
        }
        赶上(例外EE)
        {
            //如果我们有一个问题,只需返回null
            返回null;
        }
    }


私人无效UpdateDisplay()
    {
        TextView的feedtitle =(TextView中)findViewById(R.id.feedtitle);
        TextView的feedpubdate =(TextView中)findViewById(R.id.feedpubdate);
        ListView控件ITEMLIST =(ListView控件)findViewById(R.id.itemlist);


        如果(原料== NULL)
        {
            feedtitle.setText(暂无RSS feed可用);
        返回;
        }

        feedtitle.setText(feed.getTitle());
        feedpubdate.setText(feed.getPubDate());

        ArrayAdapter<的RSSItem>适配器=新的ArrayAdapter<的RSSItem>(这一点,android.R.layout.simple_list_item_1,feed.getAllItems());

        itemlist.setAdapter(适配器);

        itemlist.setOnItemClickListener(本);

        itemlist.setSelection(0);

        }
 

解决方案

您必须使用ASYC任务的 http://developer.android.com/reference/android/os/AsyncTask.html 的在活动的OnCreate功能。 记住,你必须创建一个接口(这里我用EVRequestCallback),通过它您需要RSS装载完成后,更新活动的用户界面。下面是示例$ C $的异步任务,RSS提要℃。

 公共类RetrieveRssAsync {


        公共RetrieveRssAsync(上下文克拉,EVRequestCallback GT)
        {

        }

          公共静态抽象类EVRequestCallback {
                公共抽象无效requestDidFail(ArrayList中< EventItem> EI);
                公共抽象无效requestDidLoad(ArrayList中< EventItem> EI);
          }
          公共静态类RetrieveEventFeeds扩展的AsyncTask<虚空,虚空,ArrayList的< EventItem>>
        {
              语境mContext;
              私人EVRequestCallback mCallback;
            公共RetrieveEventFeeds(上下文克拉,EVRequestCallback GT)
            {
                mContext = CT;
                mCallback = GT;
            }
            私人ProgressDialog进度= NULL;

            @覆盖
            受保护的ArrayList< EventItem> doInBackground(空... PARAMS){

                返回retrieveRSSFeed( -  RSS的网址在这里 - ,this.mContext);


            }

            @覆盖
            保护无效onCancelled(){
                super.onCancelled();
            }

            @覆盖
            在preExecute保护无效(){
                进度= ProgressDialog.show(
                        mContext,空,载入中...,真正的,真实的);

                super.on preExecute();
            }

            @覆盖
            保护无效onPostExecute(ArrayList中< EventItem>的结果){
            // setListAdapter();
                mCallback.requestDidLoad(结果);
                progress.dismiss();
                //Toast.makeText(this.mContext,目前做的,Toast.LENGTH_SHORT).show();
                super.onPostExecute(结果);
            }

            @覆盖
            保护无效onProgressUpdate(空...值){
                super.onProgressUpdate(值);
            }
        }


    }
 

I searched everywhere for this but no one appears to have an answer.

My simple question is:

Is there a way to preload an activity? I need this because I use a tab,and a tab has multiple activities. One of my activities is a rss reader,and it loads pretty hard(about 2-3 seconds).

All I found on the web is a joke.Everyone has an opinion,but no one can point you to a sample code. Waiting for an answer,thanks!

This is the code that loads the feed:

At onCreate:
// go get our feed!
        feed = getFeed(RSSFEEDOFCHOICE);

        // display UI
        UpdateDisplay();

        countdown();


And the functions:

private RSSFeed getFeed(String urlToRssFeed)
    {
        try
        {
            // setup the url
           URL url = new URL(urlToRssFeed);

           // create the factory
           SAXParserFactory factory = SAXParserFactory.newInstance();
           // create a parser
           SAXParser parser = factory.newSAXParser();

           // create the reader (scanner)
           XMLReader xmlreader = parser.getXMLReader();
           // instantiate our handler
           RSSHandler theRssHandler = new RSSHandler();
           // assign our handler
           xmlreader.setContentHandler(theRssHandler);
           // get our data via the url class
           InputSource is = new InputSource(url.openStream());
           // perform the synchronous parse           
           xmlreader.parse(is);
           // get the results - should be a fully populated RSSFeed instance, or null on error

           return theRssHandler.getFeed();
        }
        catch (Exception ee)
        {
            // if we have a problem, simply return null
            return null;
        }
    }


private void UpdateDisplay()
    {
        TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
        TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
        ListView itemlist = (ListView) findViewById(R.id.itemlist);


        if (feed == null)
        {
            feedtitle.setText("No RSS Feed Available");
        return;
        }

        feedtitle.setText(feed.getTitle());
        feedpubdate.setText(feed.getPubDate());

        ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems());

        itemlist.setAdapter(adapter);

        itemlist.setOnItemClickListener(this);

        itemlist.setSelection(0);

        }

解决方案

You have to use Asyc Task http://developer.android.com/reference/android/os/AsyncTask.html in the OnCreate function of the activity . And remember you have to create a interface(here I used EVRequestCallback) by which you need to update the UI of Activity after completion of rss loading. Below is the sample code of Async task for RSS feed.

public class RetrieveRssAsync {


        public RetrieveRssAsync(Context ct,EVRequestCallback gt)
        {

        }

          public static abstract class EVRequestCallback {
                public abstract void requestDidFail(ArrayList<EventItem> ei);
                public abstract void requestDidLoad(ArrayList<EventItem> ei);
          }
          public static class RetrieveEventFeeds extends AsyncTask<Void, Void, ArrayList<EventItem>>
        {
              Context mContext;
              private EVRequestCallback mCallback;
            public RetrieveEventFeeds(Context ct,EVRequestCallback gt)
            {
                mContext= ct;
                mCallback=gt;
            }
            private ProgressDialog progress = null;

            @Override
            protected ArrayList<EventItem> doInBackground(Void... params) {

                return retrieveRSSFeed("--URL of RSS here--",this.mContext);


            }

            @Override
            protected void onCancelled() {
                super.onCancelled();
            }

            @Override
            protected void onPreExecute() {
                progress = ProgressDialog.show(
                        mContext, null, "Loading ...",true,true);

                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(ArrayList<EventItem> result) {
            //setListAdapter();
                mCallback.requestDidLoad(result);
                progress.dismiss();
                //Toast.makeText(this.mContext, "current done", Toast.LENGTH_SHORT).show();
                super.onPostExecute(result);
            }

            @Override
            protected void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);
            }
        }


    }

这篇关于如何preLOAD的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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