How to preload an Activity?

徘徊边缘 提交于 2019-12-05 07:07:40

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);
            }
        }


    }

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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!