How to preload activity?

I searched everywhere for this, but no one has an answer.

My simple question is:

Is there a way to preload activity? I need this because I use a tab, and the tab has several actions. One of my actions is an rss reader, and it loads quite difficult (about 2-3 seconds).

All that I found on the Internet is a joke. Everyone has an opinion, but no one can give you sample code. I'm waiting for an answer. Thank you!

This is the code loading 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); } 
+6
source share
2 answers

You must use the Asyc Task http://developer.android.com/reference/android/os/AsyncTask.html in the OnCreate function for this operation. And remember that you need to create an interface (here I used EVRequestCallback), with which you need to update the user interface of Activity after the rss download is complete. The following is an example Async task code for an 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); } } } 
+1
source

The Styling Android blog provides detailed examples showing how to perform background tasks. FYI.

References:

Android Styling> Background Tasks - Part 1 - http://blog.stylingandroid.com/archives/833
Styling Android - http://blog.stylingandroid.com/

0
source

Source: https://habr.com/ru/post/901879/


All Articles