Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have one activity where i use two onActvityResults

one for the CalendarView:

@Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) 

one for the retrieving of events in Google Calendar:

    @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data)

How do i have these two in one activity? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
117 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

 public class MainActivity extends Activity {

        // Use a unique request code for each use case 
        private static final int REQUEST_CODE_EXAMPLE = 0x9988; 

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Create an Intent to start AnotherActivity
            final Intent intent = new Intent(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.--------- //
        @Override
        public void onActivityResult(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 successful
                if(resultCode == Activity.RESULT_OK) {
                    // Get the result from the returned Intent
                    final String result = 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

public class AnotherActivity extends Activity {

        // Constant used to identify data sent between Activities.
        public static final String EXTRA_DATA = "EXTRA_DATA";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_another);

            final View button = findViewById(R.id.button);
            // When this button is clicked we want to return a result
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // Create a new Intent as container for the result
                    final Intent data = new Intent();

                    // 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();
                }
            });
        }

        @Override
        public void onBackPressed() {
            // 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 ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...