java 尝试了所有但 android 完成();活动不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33514499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:47:43  来源:igfitidea点击:

Tried everything but android finish(); activity not working

javaandroidandroid-activity

提问by MabrurChowdhury

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.

我们想尽一切办法完成一项特定的活动,但未能如愿。代码运行没有任何错误或警告,只是无法完成活动。我们与其他论坛一起尝试了 stackoverflow 中的所有解决方案。需要一个带有解释的解决方案。

Android finish() Activity Not working

Android完成()活动不工作

Android finishing activity not working

Android 整理活动不起作用

android finish activity context

android完成活动上下文

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";

    }

回答by Mauker

Try to create a method on your Activitythat will call finish(). And then, call this method from within your onResponse().

尝试在您的设备上创建一个Activity将调用的方法finish()。然后,从您的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.
}

回答by Venkatesh Selvam

Check the following in your code

在您的代码中检查以下内容

1.Are you extends Activity correctly?

1.你是否正确地扩展了Activity?

Public class YourActivity extends Activity{
  }

2.if you extends activity correctly, Finish() will work properly.

2.如果您正确扩展活动,Finish() 将正常工作。

YourActivity.this.finish();

3.if you use Dialogs, you should dismiss the Dialog before finish the activity

3.如果你使用对话框,你应该在完成活动之前关闭对话框

Dialog.dismiss();

Hope may be its helpful.

希望可能会有所帮助。

Happy Coding :)

快乐编码:)

回答by Abeer Iqbal

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.

如果您在意图结果中传输大数据,则它可能无法完成活动。例如,当我将字节数组放在大尺寸的图像上时,完成不起作用,当我更改图像尺寸时,相同的代码有效。