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);
When using intent to transfer data, 1MB is not the upper security limit. Because other transport work may be in progress in binder. The upper limit may be different for different models and system versions.
Therefore, the maximum length of String in Java is 0 to 2147483647. So, we can have a String with the length of 2,147,483,647 characters, theoretically.
So you can have a String of 2,147,483,647 characters, theoretically.
To pass the data through Intent we will use putExtra() method and in parameter, we will use Key-Value Pair. Now, where we have to mention putExtra() method? We have to add putExtra() method in onClick() as shown in the below code and in parameter we have to mention key and its value.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With