Opening an Activity from a class

Viewed 52

I have created a class called UserAuthentication for setting up login and sign in. I am using volley to communicate with the database.
Everything works fine except that I cannot open the next activity when the operation is successful.

I have tried this but nothing happens :

Intent intent = new Intent(context,NextActivity.class);
context.startActivity(intent);

Also, when I type context.startActivity() Android Studio prompts me to add @RequiresPermission but when i do add it, it gives me an error.

UserAuthentication Class -> SignUp() code :

    public void SignUp(String userName,String userSignUpEmail, String userSignUpPassword){
        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                Constants.URL_SIGNUP,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            result = jsonObject.getString("message");
                            if(Objects.equals(result, "Sign Up Successful")){
                                Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
                                SharedPrefManager.getInstance(context).userSignIn(
                                        jsonObject.getInt("userId"),
                                        jsonObject.getString("userName"),
                                        jsonObject.getString("userEmail"));
                                Intent intent = new Intent(context,NextActivity.class);
                                context.startActivity(intent);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
//                        progressDialog.dismiss();
                        Log.d("SignUpError",error.getMessage());
                    }
                }){
            @Override
            protected Map<String, String> getParams() {
                Map<String,String> params = new HashMap<>();
                params.put("userName",userName);
                params.put("userSignUpEmail",userSignUpEmail);
                params.put("userSignUpPassword",userSignUpPassword);
                return params;
            }
        };
        RequestHandler.getInstance(context).addToRequestQueue(stringRequest);
    }

In my Activity where I call my class function :

signUpButton.setOnClickListener(v -> {
            String userName = usersignUpName.getText().toString();
            String userSignUpEmail = usersignUpEmail.getText().toString();
            String userPassword = userpassword.getText().toString();
            String userConfirmPassword = userconfirmPassword.getText().toString();
            if(userPassword.equals(userConfirmPassword)) {
                 UserAuthentication userAuthentication = new UserAuthentication(SignUpActivity.this);
                 userAuthentication.SignUp(userName, userSignUpEmail, userPassword));
            }
            else{
                Toast.makeText(SignUpActivity.this, "Passwords do not match !", Toast.LENGTH_SHORT).show();
            }
        });
2 Answers

You can try with passing Activity Context to your SignUp parameter. as you know Intent is only work with Current Context of Activity.

i.e

UserAuthentication userAuthentication = new UserAuthentication(SignUpActivity.this);
                 userAuthentication.SignUp(activityContext ,userName, userSignUpEmail, userPassword));

There was no error in the Intent and in startActivity().
I was trying to access a string from the JSONObject in SharedPrefManager when I had not passed any in my PHP code.
These statements works just fine -

Intent intent = new Intent(conext,NextActivity.class);
startActivity(intent);
Related