【发布时间】:2011-06-30 13:45:31
【问题描述】:
我有一个活动,我在其中显示存储在网站上的图像。 我正在使用以下代码从其 url 获取它并显示活动:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
setContentView(R.layout.ad_screen);
AsyncTask<String, Void, Integer> task = new AsyncTask<String, Void, Integer>()
{
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
@Override
protected Integer doInBackground(String... urls)
{
fetchAd();
return 0;
}
/** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
@Override
protected void onPostExecute(Integer result)
{
displayAd();
}
};
task.execute("");
}
效果很好,但行为不是我想要的:在这种情况下,活动被推送到屏幕上(带有从右到左的动画),然后 AsyncTask 开始。所以图像在延迟后显示在屏幕上(这是正常的)。
但是我想在activity被推送之前执行请求,这样屏幕就直接和它的图像一起显示,没有任何延迟。
有没有办法让这种行为? 提前致谢。
【问题讨论】:
标签: android android-activity android-asynctask