Passing Data To Other Activity Using Intent

Intents are useful for passing information around the android structure. You can communicate with your own Activities and even other processes.

You can utilize Intents, which are messages sent between activities. In a goal you can put all kind of information, String, int, and so forth.There are two conditions conceivable when we talk about passing information between activities. Expect there are two activities A and B and there is a String X. in like manner, you are in Activity A.

In Activity A:

Intent i= new Intent(this, B.class);
i.putExtra(key, stringvalue);
startActivity(i);

In Activity B:

You at that point can get this data in the Second activity by getting the Intent and separating the string extra. Do this in your onCreate() method.

Intent i= getIntent();
String res= i.getStringExtra(key, value);

or simply

String res=getIntent().getStringExtra(key,value);

Pass Data From Activity B to Activity A:

This case is minimal precarious if u are new to Android development Because you are in Activity A, you move to Activity B, gather the string, move back to Activity A and recover the gathered String or information. We should perceive how to manage this circumstance.

In Activity A:

  1. Create Intent
  2. Start an activity with a request code.
Intent i = new Intent(A.this, B.class);
startActivityForResult(i,your_req_code);

In Activity B:

  • Put string X in intent
  • Set result
  • Finish activity
Intent returnIntent = new Intent();
returnIntent.putString("KEY",X);
setResult(resCode,returnIntent);   // for the first argument, you could set Activity.RESULT_OK or your custom resCode too
finish();

Again in Activity A:

Override onActivityResult method

onActicvityResult(int req_code, int res_code, Intent data){
       if(req_code==your_req_code)
       {
          String X = data.getStringExtra("KEY")
       }
}

You may wonder what is the reqCode, resCode in the onActicvityResult(int reqCode, resCode, Intent information)

reqCode is helpful when you need to distinguish from which activity you are getting the result from.

Suppose you have two buttons, one button begins Camera (you click a photograph and get the bitmap of that picture in your Activity thus), another button begins GoogleMap( you get back the present directions of your area therefore). So to recognize the consequences of the two activities you start CameraActivty and MapActivity with various reqCodes.

resCode is significant when you have to perceive how results are coming back to referencing development.

For eg: You start Camera Activity. Right when the camera activity starts, you could either snap an image or just move back to referencing activity without snapping an image with the back button press. So in these two conditions, your camera activity sends result with different resCode ACTIVITY.RESULT_OK and ACTIVITY.RESULT_CANCEL separately.

Declaring the Activity in the Manifest:

<manifest … >
  <application … >
      <activity android:name=”.A” /> <activity android:name=”.B” />
      …
  </application … >
  …
</manifest >      

Leave a Reply

Your email address will not be published. Required fields are marked *