preface
Intents can be used to transfer data between Android components. However, when a large number of data is transferred (such as a list size>1000), intents cannot be used and will crash if they are used at all: TransactionTooLargeException. The official document says this:
When sending data through intEnts, be careful to limit the data size to a few kilobytes. Send too much data may lead to throw TransactionTooLargeException system anomalies.
Intents fail to deliver big data because of their internal use of the Binder communication mechanism, which limits the size of the data that can be delivered. Binder transaction buffers are limited to 1MB in size, but this size is shared, meaning that passing data below 1MB is not always safe, depending on the current environment. Do not push the limits of how big an Intent can be. For big data, such as long strings, bitmaps, etc., do not consider delivering data in an Intent. Several alternative solutions are described below.
Use the singleton
The code is as follows, which is relatively simple and will not be described here, just set on pass and get on fetch.
public class MusicListHolder {
private ArrayList<MusicInfo> musicInfoList;
public ArrayList<MusicInfo> getMusicInfoList() {
return musicInfoList;
}
public void setMusicInfoList(ArrayList<MusicInfo> musicInfoList) {
this.musicInfoList = musicInfoList;
}
private static final MusicListHolder holder = new MusicListHolder();
public static MusicListHolder getInstance() {
returnholder; }}Copy the code
Note: This method cannot be used with multiple processes, because the singleton retrieved by different processes is not the same singleton, i.e., no data is retrieved.
Use the EventBus
EventBus is an Android-optimized Publish/ Subscribe message bus that simplifies communication between components within an application and between components and background threads. There are also recommendations in the “Alibaba Android Development Manual” : “The Activity, the data communication between the data quantity is big, avoid to use Intent + Parcelable way, consider EventBus alternatives, such as, lest cause TransactionTooLargeException.” . For details, see EventBus
Use the Application
Save data in Application and retrieve it when needed. So that the entire application can read and write the data. It is very convenient to use, so I won’t talk about it here. But there are some problems to pay attention to when using.
Sometimes, due to insufficient memory, our application will be forcibly killed by the system. When you click to enter the application again, the system will directly enter the interface before being killed. However, the Application is newly created, so we cannot retrieve the data previously accessed. If we do not make a judgment, it will cause the problem of empty objects.
Usage suggestions:
- Use must do a good non – empty judgment
- If the data is empty, consider logically returning the application directly to the original Activity.
Persistent data
Save the data in a file. Common methods include SQLite, shared preference, and file.
Advantages:
- It’s accessible anywhere in the application
- Not easily lost
Disadvantages:
- Operating trouble
- inefficient
- Error-prone reading