0

on onPostExecute I get results {"STATUS": true} if I managed to input the data, {"STATUS": false} if the input fails.

I want to make if {"STATUS": true} then move to the LoginActivity class

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);

if {"STATUS": false} then move to the RegisterActivity class

Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(intent);

how the source code should I write and put where ?

Here is the code AsynTask.

private void registerNewMember(){

    // Ubah setiap View EditText ke tipe Data String
    final String memberId = mUsername.getText().toString().trim();
    final String phoneType = mPhoneType.getText().toString().trim();
    final String email = mEmail.getText().toString().trim();
    final String name = mNamaPengguna.getText().toString().trim();
    final String pwd = mPassword.getText().toString().trim();

    // Pembuatan Class AsyncTask yang berfungsi untuk koneksi ke Database Server
    class registerNewMember extends AsyncTask<Void,Void,String> {

        ProgressDialog loading;
        String res;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(RegisterActivity.this, "Registering Account...","Wait...",false,false);
        }

        @Override
        protected String doInBackground(Void... v) {
            HashMap<String,String> params = new HashMap<>();

            // Sesuaikan bagian ini dengan field di tabel
            params.put(KEY_MEMBER_ID, memberId);
            params.put(KEY_PHONE_TYPE, phoneType);
            params.put(KEY_EMAIL, email);
            params.put(KEY_NAME, name);
            params.put(KEY_PWD, pwd);

            RequestHandler rh = new RequestHandler();
            res = rh.sendPostRequest(GlobalVariabel.URL_REGISTER_NEW_MEMBER, params);
            Log.d("info", res.toString());
            return res;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(RegisterActivity.this, s, Toast.LENGTH_LONG).show();

        }
    }

    // Jadikan Class registerNewMember Sabagai Object Baru
    registerNewMember register = new registerNewMember();
    register.execute();
}
Drians
  • 9
  • 3
  • what do you mean "move a class"? – Stultuske Oct 10 '16 at 10:29
  • I guess you are already redirecting to Login page on {"STATUS":true}. Also is the method registerNewMember() within your activity? – Android Mason Oct 10 '16 at 10:45
  • @Stultuske yes move a login activity class at on PostExecute – Drians Oct 10 '16 at 10:47
  • @AndroidMason yes, I've been putting registerNewMember() in the login button at my Activity. but when I execute successfully, the results are not moving to LoginActivity class – Drians Oct 10 '16 at 10:59
  • @Drians do you get any error? or do you want to pass data along with redirecting your activity? your query isn't very clear. – Android Mason Oct 10 '16 at 11:11
  • @AndroidMason i didn`t get error, I want when I managed to input data to the server, the class will move to the login Activity class, when it failed to keep the Register class Activity. if(s.trim().equalsIgnoreCase("true")) { Intent intent = new Intent(getApplicationContext(), LoginActivity.class); startActivity(intent); } – Drians Oct 11 '16 at 01:49
  • @Drians did you try ? { Intent intent = new Intent(RegisterActivity.this, LoginActivity.class); startActivity(intent); } – Android Mason Oct 11 '16 at 04:52
  • @AndroidMasson the result remains the same, unsuccessful move to class LoginActivity :D – Drians Oct 11 '16 at 07:37
  • @Drians kindly share your code for LoginActivity.java – Android Mason Oct 11 '16 at 14:10

2 Answers2

0
        if(s.trim().equalsIgnoreCase("true")) {
              Intent intent =new Intent(RegisterActivity.this,LoginActivity.class);
                        startActivity(intent);
        }
        else{
                   new AlertDialog.Builder(RegisterActivity.this)
                       .setTitle("ProjectName")
                       .setMessage("Your Message")
                       .setNegativeButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                             public void onClick(DialogInterface dialog, int which) {

                             mUsername.setText("");
                             mPhoneType.setText("");
                             mEmail.setText("");
                             mNamaPengguna.setText("");
                             mPassword.setText("");

                             dialog.dismiss();
                             }
                         })
                      .show();


        }
0

Please look at this answer by @Samuh

One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method.

//To pass:
intent.putExtra("MyClass", obj);

// To retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
Community
  • 1
  • 1
Dhaval Jivani
  • 9,467
  • 2
  • 48
  • 42