美文网首页
Intent传值过大问题

Intent传值过大问题

作者: 不会弹钢琴de大叔 | 来源:发表于2023-11-19 13:54 被阅读0次

在项目开发中如果页面之间需要传递一个很大的数据(如bitmap,一个很大的数组)就可能导致出现数据过大的Runtime异常(TransactionTooLargeException),那么就需要对数据进行一下处理,使其可以正常传递过去,比如使用bundle:
一 定义Binder类,用于传递数据

import android.os.Binder;

import java.util.ArrayList;

public class BigBinder extends Binder {
    /**
     * 需要传递的数据,可以是任意类型
     */
    private ArrayList<String> list;

    public BigBinder(ArrayList<String> list){
        this.list = list;
    }

    public ArrayList<String> getData() {
        return list;
    }
}

二 send数据,通过bundle

        Intent intent = new Intent(this, XXXX.class);
        Bundle bundle=new Bundle();
        BigBinder bigBinder=new BigBinder(bigData);
        bundle.putBinder("binder",bigBinder);
        intent.putExtra("data",bundle);
        startActivity(intent);

三 接收数据

 Bundle bundle=intent.getBundleExtra("data");
 BigBinder bigBinder = (BigBinder) bundle.getBinder("binder");        
 bigBinder.getData();

至此就可以实现大数据通过intent bundle的传值。

相关文章

网友评论

      本文标题:Intent传值过大问题

      本文链接:https://www.haomeiwen.com/subject/ufhmwdtx.html