I am making an android app which has 2 activities. In this app, activity number 1 will start activity number 2 and request data from activity number 2. I use intent to transfer data between them. As I know, activity 2 only send data after finish() function is called. But In my app, I want to keep activity 2 always active. So that, is there any way to send data by using intent with out waiting finish() function ?
UPDATE: Here is my source code:
- In Activity number 1:
Open Activity number when button is pressed
public static final int REQUEST_CODE_INPUT=10;
public static final int RESULT_CODE_1=20;
public static final int RESULT_CODE_2=21;
private String TAG = "FirstActivity";
btnInputData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Open Activity number 2 With REQUEST_CODE_INPUT
Intent intent=new Intent(FirstActivity.this, SecondActivity.class);
//Call startActivityForResul
startActivityForResult(intent, REQUEST_CODE_INPUT);
}
});
Process received data
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
//Show Log
Log.d(TAG,"onActivityResult: requestCode = "+ requestCode+ "; resultCode = "+ resultCode);
//check if requestCode =REQUEST_CODE_INPUT
//
if(requestCode==REQUEST_CODE_INPUT)
{
//Check ResultCode from Activity 2
switch(resultCode)
{
case RESULT_CODE_1:
//value from Activity number 2
int v1= data.getIntExtra("data", 0);
arrData.add(v1*v1);
break;
case RESULT_CODE_2:
//value from Activity number 2
int v2= data.getIntExtra("data", 0);
arrData.add(v2);
break;
}
}
}
*In activity number 2
Call function send data when button is pressed
btnSave1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//
sendToFirstActivity(FirstActivity.RESULT_CODE_1);
}
});
btnSave2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//
sendToFirstActivity(FirstActivity.RESULT_CODE_2);
}
});
Function to return data to Activity number 1
public void sendToFirstActivity(int resultcode)
{
Intent intent=getIntent();
int value= Integer.parseInt(editNumber.getText()+"");
intent.putExtra("data", value);
setResult(resultcode, intent);
finish();
}
In this code, after press button the activity number 2 will close and send data to activity number 1. When I try to remove finish() function, the activity 2 will not close but not send data to activity number 1.