• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps What does requestCode and resultCode in onActivityResult refer to?

wseng92

Android Enthusiast
Good day guys. I'm new to android and now using **startActivityForResult** in my program. In my app, I have two button and two textView. The two `button` used to open the dialog. How can I check which button was pressed onActivityResult so that the TextView can be setText accordingly to the button?


Code:
   int a1 = 1;
      int a2 = 2;
   
       button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialogRadio(a1);
   
                }
            });
   
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialogRadio(a2);
   
                }
            });
   
     public void AlertDialogRadio(final int k) {  //parameter k is never used
                final CharSequence[] ClaimsModel = {"Project", "Petrol", "Car Maintenance"
                        , "Medical", "Other"};
   
                AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
                alt_bld.setTitle("Select a Claims");
                alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
                        .OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        if (item == 0) {
                            Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
                            startActivityForResult(intent, 0);
                        } else if (item == 1) {
                            Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
                            startActivityForResult(intent, 1);
                        } else if (item == 2) {
                            Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
                            startActivityForResult(intent, 2);
                        } else if (item == 3) {
                            Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
                            startActivityForResult(intent, 3);
                        } else if (item == 4) {
                            Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
                            startActivityForResult(intent, 4);
                        }
   
                        dialog.dismiss();
   
                    }
                });
                AlertDialog alert = alt_bld.create();
                alert.show();
            }
   
   
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == 1) { // if button1 was clicked
                switch (requestCode) {
                    case 0:
                        String result = data.getStringExtra("text");
                        String b = data.getStringExtra("a");
                        c.setText("            " + b + "------" + "RM " + result);
                        Toast.makeText(getActivity(), "Not completed ", Toast.LENGTH_LONG).show();
                        break;
   
                    case 1:
                        String result1 = data.getStringExtra("text");
                        String b1 = data.getStringExtra("a");
                        c.setText("            " + b1 + "------" + "RM " + result1);
                        break;
   
   
                    case 2:
                        String result2 = data.getStringExtra("text");
                        String b2 = data.getStringExtra("a");
                        c.setText("            " + b2 + "------" + "RM " + result2);
                        break;
   
                    case 3:
                        String result3 = data.getStringExtra("text");
                        String b3 = data.getStringExtra("a");
                        c.setText("            " + b3 + "------" + "RM " + result3);
                        break;
   
                    case 4:
                        String result4 = data.getStringExtra("text");
                        String b4 = data.getStringExtra("a");
                        c.setText("            " + b4 + "------" + "RM " + result4);
                        break;
                }
            }
            else if (resultCode == 2) { // if button2 was clicked
                switch (requestCode) {
                    case 0:
                        String result = data.getStringExtra("text");
                        String b = data.getStringExtra("a");
                        d.setText("            " + b + "------" + "RM " + result);
                        break;
   
                    case 1:
                        String result1 = data.getStringExtra("text");
                        String b1 = data.getStringExtra("a");
                        d.setText("            " + b1 + "------" + "RM " + result1);
                        break;
   
                    case 2:
                        String result2 = data.getStringExtra("text");
                        String b2 = data.getStringExtra("a");
                        d.setText("            " + b2 + "------" + "RM " + result2);
                        break;
   
                    case 3:
                        String result3 = data.getStringExtra("text");
                        String b3 = data.getStringExtra("a");
                        d.setText("            " + b3 + "------" + "RM " + result3);
                        break;
   
                    case 4:
                        String result4 = data.getStringExtra("text");
                        String b4 = data.getStringExtra("a");
                        d.setText("            " + b4 + "------" + "RM " + result4);
                        break;
                }
            }
        }


So my program should work like this:
If button1 was clicked....c.setText();
If button2 was clicked....d.setText();

But the program now is **nothing display on the TextView**. Did the error came from  `if (resultCode == 1)` and `else if (resultCode == 2)` ?? Thanks a lot


So my program should work like this:
If button1 was clicked....c.setText();
If button2 was clicked....d.setText();

But the program now is **nothing display on the TextView**. Did the error came from `if (resultCode == 1)` and `else if (resultCode == 2)` ?? Thanks a lot
 
Do you know how to run your application in debug mode, and set breakpoints in the code? If you want to do any serious app development, these are essentials skills to learn.
If you set a breakpoint at line 57 above, you could see the value of resultCode, and step through the code line by line, to see exactly what it does.
 
Your button setOnClickListener will not trigger the onActivityResult which is used for receiving the status of an activity that you had previously started. See http://developer.android.com/reference/android/app/Activity.html:

Starting Activities and Getting Results
The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, anIntent, which describes the activity to be executed.

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.

When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent's Activity.onActivityResult(), along with the integer identifier it originally supplied.

If you check the value of k in your AlertDialogRadio function, you'll be able to see which button you had pressed.
 
Back
Top Bottom