SoFunction
Updated on 2025-04-07

Android implements data transfer between Activities

This article describes the method of data transmission between Android Activities. Share it for your reference. The specific analysis is as follows:

First, let’s explain the startup and shutdown of the activity:

1. startActivity(Intent intent);  Start Activity
finish();  End the current activity
2. startActivityForResult(Intent intent, int requestCode);  Start Activity with the specified request code requestCode
finishActivity(int requestCode);  End the Activity started with the startActivityForResult(Intent intent, int requestCode) method
If there are multiple activities, then we want to close one of them, then we can set requestCode to close the corresponding activities.

Next, let’s talk about data transfer. The problem is as follows: If we go from page A to page B, there is some data in A, such as user information (user name, account, password, etc.), and enter page B from A, and we need to obtain this data information, how should we pass it on?

Intent provides various ways to carry additional data:

putExtras(Bundle data): Put the data packet carried by the medicine into the Intent
Bundle getExtras(): Take out the data packet carried in the Intent and gets a Bundle object
putExtra(String name, Xxx value): Store data information in the Intent according to the key-value pairs
getXxx(String name): Get the corresponding data information according to the key from the Intent
putXxx(String key, Xxx data): Put Xxx type data information into the Bundle
getXxx(String key): Get the corresponding data information of the key from the Bundle
putSerializable(String key, Serializable data): Put an object that can be serialized into the Bundle
getSerializable(String key, Serializable data): Take out a serializable object from the Bundle

With the above methods, we can pass data:
The method is as follows:

1. Click button bn in page A, and jump to page B with the following code:

int requestCode = 0;
(new OnClickListener())
{
 @Override
  public void onClick(View source)
 {
  Intent intent= new(, );
  startActivityForResult(intent, inquestCode);
  //Open the specified Activity B and wait for the returned result.  // where inquestCode is the request code  }
}

2. Add data to page B, the code is as follows:

Intent intent = getIntent();
//Get the Intent corresponding to the currently started Activity B("Data content","I am Li Ming");
//Bind data information "I am Li Ming" to the Intent(1, intent);
//Set the result code and set the activity returned after the end();

3. Get data information in A, the code is as follows:

In page A, in order to get the result returned in B, we should rewrite the onActivityResult() method in page A. When B returns the result, this method will be called:

String myData;
@override
public void onAcitivityResult(int requestCode,int resultCode,Intent intent)
//Rewrite this method to get the result returned by B in a callback mode{
  if(requestCode==0&&resultCode==1)
  {
    Bundle data = ();
    String myResultData = ("Data content");
    //In this way myResultData is the data information we want to pass from B  }
}

In summary, by using the methods in Intent, we can realize data transfer between different activities. By setting the identification code and result code, we can selectively turn on and close different activities, which is the data transfer between activities.

I hope this article will be helpful to everyone's Android programming design.