0

i’m writing an android apllication and i have two Activities. i should send the requestCode, the resultCode, and the Intent data into the other activity. I’ve managed to send the first two succesfully but how can i send „intent data ” variable into an other activity?

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {
  • 5
    Possible duplicate of [How do I pass data between activities in Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – Shahzeb Oct 05 '15 at 12:40
  • 2
    intent.putExtras(data); – Belvi Nosakhare Oct 05 '15 at 12:41
  • extract the particular info from the data intent and pass to another activity – George Thomas Oct 05 '15 at 12:43
  • see this http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android – Alok Oct 05 '15 at 12:44
  • My real problem is: it is a photo opener i try to make, which is capable to open a photo in an other activity. I have a button which can open the gallery, and i can choose a photo , but i want the phot opened in the other activity. If i showed the code would it help? thx – Howlett Logan Oct 05 '15 at 13:10

1 Answers1

1

Calling Second Activity from FirstActivity :

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("intent_data_tag","data");
startActivityforResult(intent,200);

get intent data in second activity:

String intentData = getIntent().getStringExtra("intent_data_tag","defValue");

Sending result to FirstActivity:

Intent resultIntent = new Intent();
 // TODO Add extras or a data URI to this intent as appropriate. 
 resultIntent.putExtra("result_data_tag","result_data");
 setResult(Activity.RESULT_OK, resultIntent);
 finish(); 

Get result in FirstActivity:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 200 && resultCode == RESULT_OK && data != null) {
    String resultData = data.getStringExtra("result_intent","defValue")
} 
} 

Example : MainActivity.java

import android.os.Bundle;  
import android.app.Activity;  
import android.content.Intent;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.TextView;  
public class MainActivity extends Activity {  
TextView textView1;  
Button button1;  
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
    textView1=(TextView)findViewById(R.id.textView1);  
    button1=(Button)findViewById(R.id.button1);  
    button1.setOnClickListener(new OnClickListener() {  
        @Override  
        public void onClick(View arg0) {  
            Intent intent=new Intent(MainActivity.this,SecondActivity.class);  
            startActivityForResult(intent, 2);// Activity is started with requestCode 2  
        }  
    });  
}  
 // Call Back method  to get the Message form other Activity  
@Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data)  
   {  
             super.onActivityResult(requestCode, resultCode, data);  
              // check if the request code is same as what is passed  here it is 2  
               if(requestCode==2)  
                     {  
                        String message=data.getStringExtra("MESSAGE");   
                        textView1.setText(message);  
                     }  
 }  
@Override  
public boolean onCreateOptionsMenu(Menu menu) {  
    // Inflate the menu; this adds items to the action bar if it is present.  
    getMenuInflater().inflate(R.menu.main, menu);  
    return true;  
}  

}

SecondActivity.java

import android.os.Bundle;  
import android.app.Activity;  
import android.content.Intent;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;  
public class SecondActivity extends Activity {  
EditText editText1;  
Button button1;  
@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_second);  
    editText1=(EditText)findViewById(R.id.editText1);  
        button1=(Button)findViewById(R.id.button1);  
        button1.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View arg0) {  
                String message=editText1.getText().toString();  
                Intent intent=new Intent();  
                intent.putExtra("MESSAGE",message);  
                setResult(2,intent);  
                finish();//finishing activity  
            }  
        });  
}  
@Override  
public boolean onCreateOptionsMenu(Menu menu) {  
    // Inflate the menu; this adds items to the action bar if it is present.  
    getMenuInflater().inflate(R.menu.second, menu);  
    return true;  
}  
}  
Er.Rohit Sharma
  • 696
  • 5
  • 21