Android uses a lot of Intent summarizes the data transfer problem

Foreword

We usually transfer data between components of general use Android Intent can be solved, but the data transfer is large (such as a size> list 1000), Intent can not be used, if you must use, then it will collapse: TransactionTooLargeException. Intent can not deliver big data is used because of its internal communication mechanisms Binder, Binder limits the size of the transaction buffer to transfer data. Binder transaction buffer size is limited to 1MB, but this size is shared, that is not passed below 1MB of data is absolutely safe, depending on the current environment. Do not challenge the limits of Intent to transfer data size, for large data, such as long strings, Bitmap, etc., do not consider Intent transfer program data. Here are some solutions.

Singleton

The following code, is relatively simple, this is not more introduced, only set in when passing, get in acquiring.

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() {
        return holder;
    }
}
复制代码

Note: This method is not available for multi-process, because different processes to get a single case of a single case is not the same, that is, not obtain data.

Use EventBus

Is a side EventBus Android optimized Publish / subscribe message bus, between the various components simplifies the communication between an application component and a background thread. In the "Ali Baba Android Development Manual" also has recommended: "Data communication between Activity, for the large amount of data, avoiding the use of Intent + Parcelable way, consider EventBus and other alternatives to avoid TransactionTooLargeException.". See specific use: EventBus

Use Application

The data stored in the Application, when taken out of use. Such applications can read and write the whole data. Very easy to use, this is not to talk about it. But there are some problems when used to pay attention to.

Sometimes because of lack of memory, etc., our application will be forced to kill the system, when this time click into the application again, the system will go directly to the interface was killed before he died. Application at this time but it is newly created, we will not be able to get access to data before, if not determine, the problem will result in an empty object.

Recommendations:

  • Be sure to use good judgment is not empty
  • Let the application if data is empty, consider the logic directly back to the original Activity.

Persistent data

The data stored in the file. Common means there sqlite, shared preference, file and so on.

Advantages :

  • Applications can access all places
  • You will not easily be lost

Disadvantages :

  • Operational problems
  • low efficiency
  • Read error-prone

Guess you like

Origin juejin.im/post/5d8de547e51d45781f73bacc