我是在这里提问的新手,所以任何关于此的建议都将不胜感激......但我的问题是:
我正在尝试从一项活动切换到另一项活动,我将在其中显示来自服务器的一些数据。我正在通过 AsyncTask 从服务器中提取数据,但是当下载时间较长时,活动之间会显示黑屏。我已经尝试将 AsyncTask 从 onCreate() 放到 onStart() 但那里没有变化。
新活动:
public class SalesMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.sales_menu);
setProgressBarIndeterminateVisibility(true);
super.onCreate(savedInstanceState);
}
@Override
protected void onStart() {
List<HashMap<String, Object>> result = null;
AsyncTask<Void, Boolean, List<HashMap<String, Object>>> aTask = new AsyncSearchRead(
"sale.order",
new Object[0],
new String[] {"name"},
this).execute();
try {
result = aTask.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Log.d("test", (result!=null ? result.toString() : "nothing"));
super.onStart();
}
}
我的异步任务:
public class AsyncSearchRead extends AsyncTask<Void, Boolean, List<HashMap<String, Object>>> {
String mModel;
boolean mCount;
Integer mOffset;
Integer mLimit;
String mOrder;
boolean mReverseOrder;
Object[] mConditions;
String[] mFields;
Activity parentActivity;
public AsyncSearchRead(String model, boolean count, Integer offset, Integer limit,
String order, boolean reverseOrder, Object[] conditions, String[] fields,
Activity activity) {
mModel = model;
mCount = count;
mOffset = offset;
mLimit = limit;
mOrder = order;
mReverseOrder = reverseOrder;
mConditions = conditions;
mFields = fields;
parentActivity = activity;
}
@Override
protected List<HashMap<String, Object>> doInBackground(Void... params) {
Long[] ids;
List<HashMap<String, Object>> results;
//Downloading the data
ids = SetServer.connection.search(mModel, mCount, mOffset, mLimit, mOrder, mReverseOrder, mConditions);
results = SetServer.connection.read(mModel, ids, mFields);
return results;
}
@Override
protected void onPostExecute(List<HashMap<String, Object>> result) {
parentActivity.setProgressBarIndeterminateVisibility(false);
}
}
当我没有关闭 onPostExecute 的可见性时,它在显示活动后继续旋转,但仅在加载后显示。
我怎样才能让它显示活动,然后才开始加载数据并显示一些进度条/加载器?
谢谢你的帮助。