Android, how to pass data from async task called from main activity, to secondary activity?
My 开发者_如何学运维application has a main activity "A", and a secondary one, "B" (called on a button click). Just before activity B is started, from activity A I run an async task to get xml data from a remote web service. How do I then pass xml data to activity B? I can't use something like
intent.putExtra("xmlData", xmlData);
since, when intent is started, xmlData is empty, yet...
Or, am I using a wrong approach? I decided to run the async task from activity A, instead than B, since I suppose I can save some time waiting for remote data in activity B...
Send the url, or unique data such as id if the url is the same, to the Activity B from A through intent.putExtra(), and fetch the data with AsyncTask as you onCreate the B activity. A progressDialog in onPreExecute might be in place as well.
Then you can reuse the Activity B if there is any similar task e.g. fetch, parse and present.
Create the intent and fire it off in startActivity inside onPostExecute() after your AsyncTask completes.
Also, is xmlData a String? That's fine, if so, otherwise you need to implement Parcelable. As an aside, you should be careful with your identifiers. A typo or incorrect case in the identifier you pass to putExtra could cause you to not find it.
精彩评论