Is there a max limit to the string data that can be passed in intent extra? How much data can the below String str hold?
intentI1.putExtra("MyString", str);
Is there a max limit to the string data that can be passed in intent extra? How much data can the below String str hold?
intentI1.putExtra("MyString", str);
My tests on Android API Level 24:
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
// intent.putExtra("extra", new byte[1024 * 1024]); // 1024 KB = 1048576 B, android.os.TransactionTooLargeException
// intent.putExtra("extra", new byte[1024 * 512]); // 512 KB = 524288 B, android.os.TransactionTooLargeException
// intent.putExtra("extra", new byte[1024 * 506]); // 506 KB = 518144 B, android.os.TransactionTooLargeException
// intent.putExtra("extra", new byte[1024 * 505]); // 505 KB = 517120 B, android.os.TransactionTooLargeException
intent.putExtra("extra", new byte[1024 * 504]); // 504 KB = 516096 B, OK
startActivity(intent);
android.os.TransactionTooLargeException https://developer.android.com/reference/android/os/TransactionTooLargeException.html
Because I need to send large amount of data to the activity, I am using the following solution (i know, its not perfect, but it can help):
public class ExtendedDataHolder {
private static ExtendedDataHolder ourInstance = new ExtendedDataHolder();
private final Map<String, Object> extras = new HashMap<>();
private ExtendedDataHolder() {
}
public static ExtendedDataHolder getInstance() {
return ourInstance;
}
public void putExtra(String name, Object object) {
extras.put(name, object);
}
public Object getExtra(String name) {
return extras.get(name);
}
public boolean hasExtra(String name) {
return extras.containsKey(name);
}
public void clear() {
extras.clear();
}
}
Then in MainActivity
ExtendedDataHolder extras = ExtendedDataHolder.getInstance();
extras.putExtra("extra", new byte[1024 * 1024]);
extras.putExtra("other", "hello world");
startActivity(new Intent(MainActivity.this, DetailActivity.class));
and in DetailActivity
ExtendedDataHolder extras = ExtendedDataHolder.getInstance();
if (extras.hasExtra("other")) {
String other = (String) extras.getExtra("other");
}
Checkout this post which says 1MB is a limit. Also checkout this one.
There is also a report on issues site.
The real question is: why pass a 1M data between activities? Perhaps a better way to achieve what you want is to persist this data and pass an identifier instead.
Because startActivity
will finally pass the whole Intent
data to ActivityManagerService
through Binder
. And the Binder
transaction buffer has a limited fixed size, currently 1Mb. Google Ref
As per Android reference:
Sending data between activities
When sending data via an intent, you should be careful to limit the data size to a few KB. Sending too much data can cause the system to throw a TransactionTooLargeException exception.
Also its advisable to use Bundle class to set primitives known to the OS on Intent objects.
And to send composite or complex objects across activities. In such cases, the custom class should implement Parcelable.
Sending data between processes
The Binder transaction buffer has a limited fixed size, currently 1MB, which is shared by all transactions in progress for the process. Since this limit is at the process level rather than at the per activity level, these transactions include all binder transactions in the app such as onSaveInstanceState, startActivity and any interaction with the system. When the size limit is exceeded, a TransactionTooLargeException is thrown.
For the specific case of savedInstanceState, the amount of data should be kept small because the system process needs to hold on to the provided data for as long as the user can ever navigate back to that activity (even if the activity's process is killed). We recommend that you keep saved state to less than 50k of data.
Note: In Android 7.0 (API level 24) and higher, the system throws a TransactionTooLargeException as a runtime exception.