0

I have two activities both xml files have an edittext and a button.I want to pass intent of first activity to second and also from second activity to first my teacher have said me to use ON ACTIVITY RESULT AND START ACTIVITY FOR RESULT METHODS

please help me i am a newcomer and its my homework!!!

1 Answers1

0

From your FirstActivity call the SecondActivity using startActivityForResult() method.

Code for first activity

Button button=(Button)findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
            startActivityForResult(intent, 0);

        }
    });

  "0"is the request code

In sec activity

  Intent intent=new Intent();
            setResult(RESULT_OK, intent);
            finish();

  Now in your FirstActivity class write following code for onActivityResult() method

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 0) {

 if(resultCode == RESULT_OK){      
     //catch the data if you had sent something from second activity.         
 }
 if (resultCode == RESULT_CANCELED) {    
     //Write your code if there's no result
 }

} }

Rhn Bhadani
  • 2,208
  • 1
  • 17
  • 25