What is?
Android uses intEnts to communicate between components.
Value transfer method:
- intent.putExtra();
Basic type: String byte short int long char Booleanfloat double
String[] byte[] short[] int[] long[] char[] boolean[] float[] Double [] Serialization: Parcelable CharSequence Serializable Parcelable[] Serializable CharSequence[]Copy the code
-
intent.putCharSequenceArrayListExtra();
-
intent.putIntegerArrayListExtra();
-
intent.putStringArrayListExtra();
-
intent.putParcelableArrayListExtra();
-
intent.putExtras(); Pass a bundle object in which all data is encapsulated
Question 1: Is there a limit to how much data an Intent can transmit? What are the limits?
- Test Case: Passing a 1M byte array with an Intent
Intent intent = new Intent(AActivity.this, BActivity.class);
byte[] bytes = new byte[1024 * 1024];
intent.putExtra("key", bytes);
startActivity(intent);
Copy the code
- Results: the abnormal TransactionTooLargeException
Caused by: android.os.TransactionTooLargeException: data parcel size 1048956 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:615)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3070)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
Copy the code
-
How it works: How much data can an Intent transmit?
-
The size of the data delivered by an Intent is limited. The specific size is (1*1024*1024) – (4096 *2).
Thought 2: If there are limitations, is there a good transmission scheme?
-
EventBus and similar schemes can be used to transfer data;
-
Static variables can be declared for data transfer; “High efficiency, high coupling”
-
Data can be persisted and then restored, using SharedPreferences or stored in the database, SP is relatively simple; ‘Inefficient’
Gson = new GsonBuilder().serializenulls ().create(); String json = gson.toJson(userEntity); Gson Gson = new GsonBuilder().serializenulls ().create(); UserEntity loginUser = gson.fromJson(jsonEntity,UserEntity.class);Copy the code
Intent uses bundles to store data. Is the Intent passed by value (deep copy) or by reference?
-
The value type of the Intent requires:
1) The data delivered by the Intent is stored in an mExtras object of type Bundle. The Bundle requires that all stored data be serializable.
2) Why does the Bundle require serialization of the data delivered here?
Because activities pass data between them, cross-process communication is a primary concern, and Binder mechanisms handle cross-process communication in Android. When it comes to cross-processes, serialization and deserialization are involved for complex data, which is bound to be a value transfer (deep copy) process.
3) Is serialization required for all data transferred through bundles?
No, serialization is only relevant for cross-process communication with Binder. Other bundles, such as those used by fragments, do not need serialization.
-
Serialization instructions:
In Android, serializing data requires either Serializable or Parcelable implementation. The wrapper class for the underlying data type itself implements Serializable, while our custom object implements either of the two serialization interfaces on demand.
-
So this problem using reduction to absurdity can explain, if it is a reference, the transfer of the past is only object references to point to the object store address, only equivalent to the size of an Int, also won’t appear abnormal TransactionTooLargeException.
Question 4: How do intEnts and bundles serialize and deserialize each other?
Thought 5: What are the key points of serialization and deserialization?
Thank you
Frequent Interviewer: How much data can an Intent convey? | pay ali’s proposal!
How much data can an Intent transmit?
Intent to transfer data is too large, TransactionTooLargeException anomalies