Skip to content Skip to sidebar Skip to footer

Java, Android: Onactivityresult With Different Parameters In One Activity

I have one activity where i use two onActvityResults one for the CalendarView: @Override protected void onActivityResult(int requestCode, int responseCode, Intent intent) on

Solution 1:

It seems you have not properly understood the concept! This might help you to understand onActivityResult.

By using startActivityForResult(Intent intent, int requestCode) you can start another Activity and then receive a result from that Activity in the onActivityResult() method.So onActivityResult() is from where you start the another Activity.

onActivityResult(int requestCode, int resultCode, Intent data) check the params here. request code is there to filter from where you got the result. so you can identify different data using their requestCodes!

Example

publicclassMainActivityextendsActivity {

        // Use a unique request code for each use case privatestaticfinalintREQUEST_CODE_EXAMPLE=0x9988; 

        @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Create an Intent to start AnotherActivityfinalIntentintent=newIntent(this, AnotherActivity.class);

            // Start AnotherActivity with the request code
            startActivityForResult(intent, REQUEST_CODE_EXAMPLE);
        }

        //-------- When a result is returned from another Activity onActivityResult is called.--------- //@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            // First we need to check if the requestCode matches the one we used.if(requestCode == REQUEST_CODE_EXAMPLE) {

                // The resultCode is set by the AnotherActivity// By convention RESULT_OK means that what ever// AnotherActivity did was successfulif(resultCode == Activity.RESULT_OK) {
                    // Get the result from the returned IntentfinalStringresult= data.getStringExtra(AnotherActivity.EXTRA_DATA);

                    // Use the data - in this case, display it in a Toast.
                    Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
                } else {
                    // AnotherActivity was not successful. No data to retrieve.
                }
            }
        }
    }

AnotherActivity <- This the the one we use to send data to MainActivity

publicclassAnotherActivityextendsActivity {

        // Constant used to identify data sent between Activities.publicstaticfinalStringEXTRA_DATA="EXTRA_DATA";

        @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_another);

            finalViewbutton= findViewById(R.id.button);
            // When this button is clicked we want to return a result
            button.setOnClickListener(newView.OnClickListener() {
                @OverridepublicvoidonClick(View view) {
                    // Create a new Intent as container for the resultfinalIntentdata=newIntent();

                    // Add the required data to be returned to the MainActivity
                    data.putExtra(EXTRA_DATA, "Some interesting data!");

                    // Set the resultCode to Activity.RESULT_OK to // indicate a success and attach the Intent// which contains our result data
                    setResult(Activity.RESULT_OK, data); 

                    // With finish() we close the AnotherActivity to // return to MainActivity
                    finish();
                }
            });
        }

        @OverridepublicvoidonBackPressed() {
            // When the user hits the back button set the resultCode // to Activity.RESULT_CANCELED to indicate a failure
            setResult(Activity.RESULT_CANCELED);
            super.onBackPressed();
        }
    }

Note : Now check in MainActivity you startActivityForResult there you specify a REQUEST_CODE. Let's say you want to call three different Activities to get results.. so there are three startActivityForResult calls with three different REQUEST_CODE's. REQUEST_CODE is nothing but a unique key you specify in your activity to uniquely identify your startActivityForResult calls.

Once you receive data from those Activities you can check what is the REQUEST_CODE, then you know ah ha this result is from this Activity.

It's like you send mails to your lovers with a colorful covers and ask them to reply in the same covers. Then if you get a letter back from them, you know who sent that one for you. awww ;)

Solution 2:

You don't. You have just one, but they should have different codes, that's how you can differentiate if the result is from Calendar view or Google Calendar or something else.

Solution 3:

You could use the requestCode parameter of onActivityResult method. Call startActivityForResult method for both the scenario using different requests code.

E.g.

private staticintCALENDAR_VIEW_CALL = 1;
private staticintCALENDAR_EVENTS_CALL = 2;

...

startActivityForResult(intent1, CALENDAR_VIEW_CALL);

...

startActivityForResult(intent1, CALENDAR_EVENTS_CALL);

Now in your onActivityResult:

publicvoidonActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == CALENDAR_VIEW_CALL) {
        //do the callback work for calendar view
    }
    elseif (requestCode == CALENDAR_EVENTS_CALL) {
        //do the callback work after retrieving the events
    }
}

Solution 4:

But you don't have methods with different parameters, you have the same method, with the same parameters, only the name of the parameters is different, and you can't do this in java, because the language doesn't really know which method is which.

You should follow the other answers and check the requestCode

Post a Comment for "Java, Android: Onactivityresult With Different Parameters In One Activity"