1.Intent对象携带简单数据
利用Intent的Extra部分来存储我们想要传递的数据,可以传送int, long, char等一些基础类型,对复杂的对象就无能为力了。
//传递些简单的参数
Intent intentSimple = new Intent();
intentSimple.setClass(MainActivity.this,SimpleActivity.class);
Bundle bundleSimple = new Bundle();
bundleSimple.putString("usr", "xcl");
bundleSimple.putString("pwd", "zj");
intentSimple.putExtras(bundleSimple);
startActivity(intentSimple);

接收参数
Bundle bunde = this.getIntent().getExtras();
String eml = bunde.getString("usr");
String pwd = bunde.getString("pwd");

2. 利用Intent对象携带如ArrayList之类复杂些的数据
这种原理是和上面一种是一样的,只是要注意下。 在传参数前,要用新增加一个List将对象包起来。
设置参数
//传递复杂些的参数
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("key1", "value1");
map1.put("key2", "value2");
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list.add(map1);

Intent intent = new Intent();
intent.setClass(MainActivity.this,ComplexActivity.class);
Bundle bundle = new Bundle();
//须定义一个list用于在budnle中传递需要传递的ArrayList<Object>,这个是必须要的
ArrayList bundlelist = new ArrayList();
bundlelist.add(list);
bundle.putParcelableArrayList("list",bundlelist);
intent.putExtras(bundle);
startActivity(intent);

//接收参数
Bundle bundle = getIntent().getExtras();
ArrayList list = bundle.getParcelableArrayList("list");
//从List中将参数转回 List<Map<String, Object>>
List<Map<String, Object>> lists = (List<Map<String, Object>>)list.get(0);
String sResult = "";
for (Map<String, Object> m : lists){
  for (String k : m.keySet()){
    sResult += "\r\n"+k + " : " + m.get(k);
  }
}

3. 通过实现Serializable接口
设置参数
利用Java语言本身的特性,通过将数据序列化后,再将其传递出去。

// 使用 LinkedHashMap 传递,接收的时候要用 HashMap 强制转换。
                    LinkedHashMap<String,String> map1 = new LinkedHashMap<>();
                    map1.put("key11", "value11");
                    map1.put("key22", "value22");

//                    HashMap<String, String> map2 = new HashMap<String, String>();
//                    map2.put("key1", "value1");
//                    map2.put("key2", "value2");

                    Bundle bundleSerializable = new Bundle();
                    bundleSerializable.putSerializable("serializable", map1);
                    Intent intentSerializable = new Intent();
                    intentSerializable.putExtras(bundleSerializable);
                    intentSerializable.setClass(MainActivity.this, TestFormActivity.class);
                    startActivity(intentSerializable);

接收参数

//接收参数
        Bundle bundle = this.getIntent().getExtras();
        //如果传 LinkedHashMap,则bundle.getSerializable转换时会报ClassCastException,需要用HashMap转化。
        //传HashMap倒没有问题。
        HashMap<String, String> map = (HashMap<String, String>) bundle.getSerializable("serializable");
        String sResult = "map.size() = " + map.size();
        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            sResult += "\r\n key----> " + (String) key;
            sResult += "\r\n value----> " + (String) value;
        }
        System.out.println("接收参数:\n" + sResult);

 参考:https://blog.csdn.net/jiangjie4558/article/details/41083827/
https://zhuanlan.zhihu.com/p/584319251?utm_id=0

posted on 2023-06-29 15:16  邢帅杰  阅读(486)  评论(0编辑  收藏  举报