We tried everything to finish a particular activity but failed to do so. The Code runs without any error or warning just cant finish the activity. We tried every solutions in stackoverflow along with other forums. Need a solution with explanation.
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";
}
Try to create a method on your Activity
that will call finish()
. And then, call this method from within your onResponse()
.
Something like:
private void killActivity() {
finish();
}
And then, call it here:
if(Boolean.parseBoolean(Flag)) {
Intent intent=new Intent(PhoneVerificationActivity.this,ConfirmSms.class);
intent.putExtra("PhoneNumber", Phone);
startActivity(intent);
killActivity(); // Here.
}
Check the following in your code
1.Are you extends Activity correctly?
Public class YourActivity extends Activity{
}
2.if you extends activity correctly, Finish() will work properly.
YourActivity.this.finish();
3.if you use Dialogs, you should dismiss the Dialog before finish the activity
Dialog.dismiss();
Hope may be its helpful.
Happy Coding :)
If you are transferring big data in intent result then it may not finish activity. e.g when i am putting byte array on image of big size, finish is not working, when i change image size same code works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With