Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transfer data between two activities [duplicate]

Tags:

java

android

I am trying to send and receive data between two different activities. I have seen some other questions asked on this site but no question has dealt with preserving the state of the first class.

For example if I want to send an integer X to class B from class A then to do some operations on integer X and then send it back to class A, how does one go about doing this?

Is it as simple as the following code?

in Class A

 Intent i = new Intent(this, ActivityB.class);
 i.putExtra("Value1", 1);
 startActivity(i);

and to receive the response from Class B:

Bundle extras = getIntent().getExtras();
int value1 = extras.getint("Value1",0);

in Class B

Bundle extras = getIntent().getExtras();
int value1 = extras.getint("Value1",0);
//Do some operations on value1 such as maybe adding or subtracting
Intent i = new Intent(this, ActivityA.class);
i.putExtra("Value1", 1);
startActivity(i);

This does not seem correct as I simply want to switch back to Activity A and receive the data from Activity B once the action is completed (perhaps a button in Activity B commences operations on the received data and then sends it back to Activity A?)

like image 744
Jewgioh Avatar asked Oct 13 '17 13:10

Jewgioh


2 Answers

In First activity :

Intent i = new Intent(this, ActivityB.class);
i.putExtra("Value1", "1");
startActivityForResult(i, 100);

Receive data like :

Intent receivedIntent = getIntent();
if(receiveIntent!=null){
   String value1 = receiveIntent.getStringExtra("Value1");
}

After some operations In second activity:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();

Handle result in FirstActivity :

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

    if (requestCode == 100) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
        }
    }
}
like image 51
Nilesh Deokar Avatar answered Oct 16 '22 17:10

Nilesh Deokar


startActivityForResult() and onActivityResult() is your solution.

In your ActivityA. Use startActivityForResult() -

Intent i = new Intent(this, ActivityB.class);
 i.putExtra("Value1", 1);
 startActivityForResult(i, requestCodeForOperation);

And on your ActivityB, get your data sent from ActivityA. Like -

int value1 = getIntent().getExtras().getInt("Value1", 0); 

Do your operation and use setResult() for adding you operation result and finish(). like-

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();

And of course you need to implement onActivityResult() on ActivityA to get returned data from ActivityB. Like -

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

    if (requestCode == requestCodeForOperation) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getIntExtra("result", 0);
        }
    }
}
like image 39
AGM Tazim Avatar answered Oct 16 '22 17:10

AGM Tazim