0

Solved Thank you guys i resolved my problem by removing PreExecute funtion from my AsyncTask function. Thanks all.

i want to return Integer value which is userId to other activities from a AsycnTask function, I tried googling my problem and saw a lot of discussions on stackoverflow but none solved my problem...Hope you guys can guide me how to do this.

Final Code: (After solving problem)

private class ValidateUserAccount extends AsyncTask<Void, Integer, Void> {
    String response = "";

    @Override
    protected Void doInBackground(Void... voids) {
        HashMap<String, String> loginDataParams = new HashMap<>();
        loginDataParams.put("email", strEmail);
        loginDataParams.put("password", strPassword);
        JSONObject jsonData = new JSONObject(loginDataParams);
        String path = "myUrl";
        response = Login.ServerData(path, jsonData);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        uProgressBar.setVisibility(View.GONE);
        int code = Login.passResCode();
        userId = Login.passUserId();
        if (code == 200) {
            Toast.makeText(getApplicationContext(), "Successfully Login: " + userId, Toast.LENGTH_SHORT).show();
            userIdSharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
            Intent loginSuccess = new Intent(getApplicationContext(), MainActivity.class);
            userIdEditor = userIdSharedPreferences.edit();
            userIdEditor.putInt("userId", userId);
            userIdEditor.apply();
            startActivity(loginSuccess);
            finish();
        } else {
            final AlertDialog.Builder alertBox = new AlertDialog.Builder(LoginActivity.this);
            String errorMessage = "";
            if (code == 401)
                errorMessage = "Email and Password are not valid.";
            else if (code == 0)
                errorMessage = "Please check your internet connection.";

            alertBox.setMessage(errorMessage).setCancelable(false)
                    .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.cancel();
                        }
                    });
            AlertDialog alert = alertBox.create();
            alert.setTitle("Oops, Something went wrong!");
            alert.show();
        }
    }
}
KhanStan99
  • 414
  • 8
  • 20

3 Answers3

1

I think you can just put it as extra in the intent... I have not really understand why you used userIdEditor and what it is... but i think something like this would work

Intent loginSuccess = new Intent(getApplicationContext(), MainActivity.class); loginSuccess.putExtra("userId",userId); startActivity(loginSuccess); And you would get the id back in the openning Activity by doing

int userId = getIntent().getExtra().getInt("userId");

Master Cy
  • 37
  • 4
  • tried this,... it shows red line which says: Cannot resolve method 'putExtras(java.lang.String, java.lang.Integer)' And i am doing this in AsycnTask fucntion. – KhanStan99 Sep 21 '17 at 10:13
  • finally got everything... you can use intent'extras and when the activity is launched save it in sharedPreferences – Master Cy Sep 21 '17 at 10:31
  • verify the method name.. have you written putExtras like in your comment? if did remove 's' – Master Cy Sep 21 '17 at 10:33
0

You can pass extras across activities using Intent#putExtra.[Read](https://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Bundle)) more here.

Sumit Jha
  • 2,095
  • 2
  • 21
  • 36
0

while writing to sharedpreference please use userIdEditor.commit(); Commit function will return boolean value thus you can check whether sharedpreference value is commited or applied successfully

Basil jose
  • 774
  • 3
  • 11