What should I do when encountering the need to transfer a large amount of data between Activities?

The data transferred between activities is generally relatively simple, but sometimes some more complex data will be transferred in actual development, especially when the interview asks what should I do when a large amount of data needs to be transferred between activities?

The size of Intent transfer data is limited. The data it can transfer is about 1M-8K. The reason is that the memory size of Binder lock mapping is 1M-8K. Generally, data transfer between activities will use binder, so this becomes The limit on the size of the data transfer. So what method should be used to transfer large data between activities? In fact, there are many ways, let's give you a few examples to explain, but it is nothing more than using data persistence or memory sharing solutions. Generally, the storage of big data is not suitable for using SP, MMKV, and DataStore.

There are several ways to transfer large amounts of data between activities:
  • LruCache
  • Persistence (sqlite, file, etc.)
  • anonymous shared memory
Use LruCache

LruCache is a caching strategy that can help us manage the cache. Students who want to know more about it can go to the Glide chapter to learn about it first. Under the current problem, we can use LruCache to store our data as a transfer. For example, we need Activity A to transfer a large amount of data to Activity B. We can write data to LruCache first in Activity A, and then read it from LruCache.

First, we define the write and read rules:

public interface IOHandler {
    //保存数据
    void put(String key, String value);
    void put(String key, int value);
    void put(String key, double value);
    void put(String key, float value);
    void put(String key, boolean value);
    void put(String key, Object value);
​
    //读取数据
    String getString(String key);
    double getDouble(String key);
    boolean getBoolean(String key);
    float getFloat(String key);
    int getInt(String key);
    Object getObject(String key);
}

We can write specific implementation classes according to the rules, that is, interfaces. In the implementation class, we save data and use LruCache. We must set a size here, because the maximum value of data in memory is determined, and the size of our stored data should not exceed 1/8 of the maximum value.

LruCache<String, Object> mCache = new LruCache<>( 10 * 1024*1024);

Writing data we use is relatively simple:

@Override
public void put(String key, String value) {
    mCache.put(key, value);
}

For example, if the data of String type is written above, all the received data needs to be put into mCache.

Reading data is also relatively simple and convenient:

@Override
public String getString(String key) {
    return String.valueOf(mCache.get(key));
}
persistent data

That is the way of sqlite, file and so on. Write the data that needs to be transferred in a temporary file or database, and then read the data information when jumping to another component. This processing method will lead to low program operation efficiency due to the time-consuming reading and writing of files. The characteristics of this method are as follows:

Advantage:

(1)应用中全部地方均可以访问

(2)即便应用被强杀也不是问题了

缺点:

(1)操做麻烦

(2)效率低下

匿名共享内存

在跨进程传递大数据的时候,我们一般会采用binder传递数据,但是Binder只能传递1M一下的数据,所以我们需要采用其他方式完成数据的传递,这个方式就是匿名共享内存。

Anonymous Shared Memory 匿名共享内存」是 Android 特有的内存共享机制,它可以将指定的物理内存分别映射到各个进程自己的虚拟地址空间中,从而便捷的实现进程间内存共享。

Android 上层提供了一些内存共享工具类,就是基于 Ashmem 来实现的,比如 MemoryFile、 SharedMemory。

今日分享到此结束,对你有帮助的话,点个赞再走呗,每日一个面试小技巧

关注公众号:Android老皮
解锁  《Android十大板块文档》 ,让学习更贴近未来实战。已形成PDF版

内容如下

1.Android车载应用开发系统学习指南(附项目实战)
2.Android Framework学习指南,助力成为系统级开发高手
3.2023最新Android中高级面试题汇总+解析,告别零offer
4.企业级Android音视频开发学习路线+项目实战(附源码)
5.Android Jetpack从入门到精通,构建高质量UI界面
6.Flutter技术解析与实战,跨平台首要之选
7.Kotlin从入门到实战,全方面提升架构基础
8.高级Android插件化与组件化(含实战教程和源码)
9.Android 性能优化实战+360°全方面性能调优
10.Android零基础入门到精通,高手进阶之路

Guess you like

Origin juejin.im/post/7264503091116965940