我们尝试了一切来完成特定的活动,但未能这样做。代码运行没有任何错误或警告只是无法完成活动。我们尝试了stackoverflow中的每个解决方案以及其他论坛。需要一个解释的解决方案。
Android finish() Activity Not working
Android finishing activity not working
android finish activity context
public void HttpSmsRequest(final String Phone){
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...";
pDialog.show();
Map<String, String> jsonParams = new HashMap<String, String>();
// jsonParams.put("param1", youParameter);
jsonParams.put("Phone", Phone);
//jsonParams.put("rememberMe", "true";
JsonObjectRequest myRequest = new JsonObjectRequest(
Request.Method.POST,
AppGlobal.host+"PhoneVerification/sendSms",
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try
{
Log.v("Success", "success: " + response.toString());
//MessageBox.Show(ProfileInfoActivity.this, "Response: " + response.toString());
pDialog.dismiss();
JSONObject obj=new JSONObject(response.toString());
// String ID=obj.getString("ID";
String Flag=obj.getString("Flag";
String Message=obj.getString("Message";
// Context context = getApplicationContext();
//
/// appPrefs.setUserIdentity(Integer.parseInt(ID));
if(Boolean.parseBoolean(Flag))
{
Intent intent=new Intent(PhoneVerificationActivity.this,ConfirmSms.class);
intent.putExtra("PhoneNumber", Phone);
//intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
return;
//finishActivity(Activity.RESULT_OK);
}
}
catch (Exception ex)
{
MessageBox.Show(context, ex.getMessage());
}
// verificationSuccess(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Log.v("Success", "Error: " + error.networkResponse.statusCode);
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
MessageBox.Show(PhoneVerificationActivity.this, "Error: " + error.toString());
AppController.getInstance().getRequestQueue().cancelAll("tag_json_obj";
}
pDialog.dismiss();
//verificationFailed(error);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map headers = new HashMap();
if (!Preference.getInstance().getCookie().equals("")
headers.put("Cookie", Preference.getInstance().getCookie());
return headers;
}
/*@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
Map headers = response.headers;
String cookie = headers.get("Set-Cookie".toString();
Preference.getInstance().saveCookie(cookie);
// MyApp.get().checkSessionCookie(response.headers);
Log.v("Success", "Response"+response.headers.toString());
return super.parseNetworkResponse(response);
}*/
};
AppController.getInstance().addToRequestQueue(myRequest, "tag_json_obj";
}
答案 0 :(得分:13)
尝试在Activity
上创建一个名为finish()
的方法。然后,在onResponse()
。
类似的东西:
private void killActivity() {
finish();
}
然后,在这里打电话:
if(Boolean.parseBoolean(Flag)) {
Intent intent=new Intent(PhoneVerificationActivity.this,ConfirmSms.class);
intent.putExtra("PhoneNumber", Phone);
startActivity(intent);
killActivity(); // Here.
}
答案 1 :(得分:1)
检查代码中的以下内容
1.您是否正确扩展了活动?
Public class YourActivity extends Activity{
}
2.如果您正确扩展活动,Finish()将正常工作。
YourActivity.this.finish();
3.如果您使用Dialogs,则应在完成活动之前关闭Dialog
Dialog.dismiss();
希望可能有所帮助。
快乐编码:)
答案 2 :(得分:0)
如果您按意向结果传输大数据,则可能无法完成活动。 例如,当我将字节数组放在大尺寸的图像上时,完成不起作用,当我更改图像尺寸时,相同的代码也可以工作。