我有两个活动,一个用于显示条目,另一个用于创建它们。这是一个标准用例:
- 用户打开应用程序并显示条目。
- 用户单击按钮创建新条目并打开新活动。
- 用户完成创建条目并单击“完成”。
- 活动以 setResult() 和 finish() 结束。
我要添加的是在查看活动完全加载后运行的 AsyncTask。此 AsyncTask 在运行时显示一个对话框。问题是,当我像这样将这段代码放入 onActivityResult() 时:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(resultCode) {
case (Activity.RESULT_OK): {
mEntryListFragment.updateContent();
runAsyncTaskHere();
}
}
}
它在返回主活动之前运行 AsyncTask,并且不显示任何对话框。有什么想法我能做什么?
编辑:这是 AsyncTask。
public static class LogInTask extends AsyncTask<String, Void, Integer> {
protected String username = "";
protected String password = "";
protected ProgressDialog dialog;
protected boolean showDialogs;
public LogInTask(boolean sd) {
this.showDialogs = sd;
}
@Override
protected void onPreExecute() {
if (this.showDialogs) dialog = ProgressDialog.show(MainActivity.getContext(), null, "Logging in...");
}
@Override
protected Integer doInBackground(String... login) {
if (Config.DEBUG) Log.d(Config.APP_NAME, "in doInBackground() of LogInTask");
HttpResponse response = null;
String username = login[0];
String password = login[1];
try {
HttpPost httpPost = new HttpPost(URL_BASE + URL_LOGIN);
// main (JSON) login method
JSONObject json = new JSONObject().put("user",
new JSONObject()
.put("email", username)
.put("password", password)
.put("remember_me", Config.REMEMBER_LOGIN)
);
this.username = username;
this.password = password;
StringEntity se = new StringEntity(json.toString());
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setEntity(se);
response = httpclient.execute(httpPost);
String responseEntity = EntityUtils.toString(response.getEntity());
JSONObject result = new JSONObject(responseEntity);
return result.getBoolean("success") ? 1 : KEY_ERROR; // Return 1 if successful and -1 if not
} catch (UnknownHostException e) {
e.printStackTrace();
KEY_ERROR;
} catch (Exception e) {
e.printStackTrace();
return KEY_ERROR;
}
}
@Override
protected void onPostExecute(Integer result) {
if (this.showDialogs) {
dialog.dismiss();
String text = result == 1 ? "Login was successful." : "Login failed!";
makeToast(text);
}
}
}
编辑2:我试过这个:
boolean needToUpdate = false;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(resultCode) {
case (Activity.RESULT_OK): {
mEntryListFragment.updateContent();
needToUpdate = true;
}
}
}
@Override
public void onResume() {
super.onResume();
if (needToUpdate) {
runAsyncTaskHere();
mEntryListFragment.updateContent();
}
needToUpdate = false;
}
它做了同样的事情。
编辑 3:这里是 AndroidManifest 的相关部分:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AddEntryActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation" />