问题
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);
}
回答1:
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);
}
}
}
回答2:
The blog "Styling Android" provides detail examples to show how to do background tasks. FYI.
Links:
Styling Android > Background Tasks – Part 1 - http://blog.stylingandroid.com/archives/833
Styling Android - http://blog.stylingandroid.com/
来源:https://stackoverflow.com/questions/8193394/how-to-preload-an-activity