在上一篇博客中介绍到,Android-Intent意图传递数据,能够传递基本数据类型系列,能够传递对象(需要序列化),等操作;

但是如果要传递 List<T>,这种类型的数据,就不能通过Intent来传递来,还有另外的方式来传递,就是自定义一个会话存储(取名:IntentSession)

IntentSession这种方式传递数据,可以通用,很灵活,能够传递任何类型的数据

IntentSession封装:

package liudeli.activity.intent;

import java.util.WeakHashMap;

public class IntentSession {

    /**
* 使用懒汉式单例模式
*/ private static IntentSession intentSession = null; public static IntentSession getInstance() {
if (null == intentSession) { // 加入同步锁
synchronized (IntentSession.class) { // 为来解决CPU极少概率,时差性,再判断一次
if (null == intentSession) {
intentSession = new IntentSession();
}
}
}
return intentSession;
} /**
* 为什么要用WeakHashMap
* HashMap的Key是对实际对象对强引用
* WeakHashMap的特点是弱引用:当gc线程发现某个对象只有弱引用指向它,就会被消耗并回收
*/
private WeakHashMap<String, Object> weakHashMap = new WeakHashMap<>(); /**
* 保存数据
* @param key
* @param obj
*/
public void put(String key, Object obj){
if (weakHashMap.containsKey(key)) {
weakHashMap.remove(key);
}
weakHashMap.put(key, obj);
} /**
* 获取数据后删除
* @param key
* @return
*/
public Object getRemove(String key) {
if (weakHashMap.containsKey(key)) {
return weakHashMap.remove(key);
}
clear();
return null;
} /**
* 获取数据但不删除
* @param key
* @return
*/
public Object get(String key){
if(weakHashMap.containsKey(key)){
return weakHashMap.get(key);
}
return null;
} /**
* 清除
*/
public void clear() {
weakHashMap.clear();
} /**
* 结束自身 自杀
*/
public void oneseifExit() {
intentSession = null;
System.gc();
}
}

在OuterActivity 启动 OneActivity,绑定数据:

     Intent intent = new Intent(this, TwoActivity.class);

        // 数据
List<String> list = new ArrayList<>();
list.add("黄家驹");
list.add("张雨生");
list.add("陈百强"); // 使用自定义的IntentSession 来绑定数据
IntentSession.getInstance().put("listData", list); startActivity(intent);

在OneActivity接收,接收IntentSession数据:

     TextView  tvInfo = findViewById(R.id.tv_info);

        // 得到绑定好的数据,并删除数据
List<String> listData = (List<String>) IntentSession.getInstance().getRemove("listData");
tvInfo.setText("" + listData.toString()); // 清空IntentSession
IntentSession.getInstance().oneseifExit();

Android-自定义IntentSession来传递数据的更多相关文章

  1. Xamarin Android 中Acitvity如何传递数据

    在xamarin android的开发中,activity传递数据非常常见,下面我也来记一下在android中activity之间传递数据的几种方式, Xamarin Android中Activity ...

  2. Android 消息广播Intent传递数据

    1.创建布局文件activity_broadcast.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

  3. Android 使用剪切板传递数据

    使用剪切板传递数据,可以传递简单的数据,也可以传递可序列化的对象. 首先来个简单点吧. 首先在,mainActivity.xml文件中加入一个button按钮 private Button butto ...

  4. android 使用静态变量传递数据

    使用静态变量传递数据之通用方式. 测试应用:当前页面点击button传递数据到一个新的页面显示在textview中. 首先在,mainActivity.xml文件中加入一个button按钮 <B ...

  5. Android学习之Intent传递数据

    Intent在Activity中的作用主要是有两个: 1.启动目标Activity 2.传递数据 Intent在传递数据时分两种情况:向下一个Activity传递数据和从下一个Activity返回数据 ...

  6. [Android] Android 最全 Intent 传递数据姿势

    我们都是用过 Intent,用它来在组件之间传递数据,所以说 Intent 是组件之间通信的使者,一般情况下,我们传递的都是一些比较简单的数据,并且都是基本的数据类型,写法也比较简单,今天我在这里说的 ...

  7. Android Activity和Fragment传递数据

    1.Activity与Activity传递数据 UserLoginActivity.java: Intent welcomePage = new Intent(); Bundle dataBundle ...

  8. WebView之js调用Android类的方法传递数据

    1,具体的思路如下: 在android中写一个Activity,里面写一个webview,这个webview加载本地的一个html文件,显示这个网页,这个网页包括一个用户名和密码的输入框和两个按钮(只 ...

  9. Android基础 -- Activity之间传递数据(bitmap和map对象)

    原文:http://blog.csdn.net/xueerfei008/article/details/23046341 做项目的时候需要用到在2个activity之间传递一些数据,之前做的都是些字符 ...

随机推荐

  1. Python 小知识点(8)-- __new__

    第一段代码如下: class Foo(object): def __init__(self,name): self.name = name print("Foo __init__" ...

  2. 神奇的make自动生成include file的功能

    嗯,今天研究公司makefile的代码,始终搞不明白有一段下载编译依赖的rule recipe(对这个名词不了解请参考make的官方文档)是怎么执行的.明明在执行的时候并指定的target并没有依赖那 ...

  3. System.Drawing.Text.TextRenderingHint 的几种效果

    ; i < ; i++) { g5.TextRenderingHint = (System.Drawing.Text.TextRenderingHint)i; string txt; ; txt ...

  4. Python 位运算符 逻辑运算符 成员运算符

    位运算符 运算符 描述 实例 & 按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0 (a & b) 输出结果12 ,二进制解释:0000 1100 | ...

  5. docker redis4.0 集群(cluster)搭建

    前言 redis集群对于很多人来说非常熟悉,在前些日子,我也有一位大兄弟也发布过一篇关于在阿里云(centOS7)上搭建redis 集群的文章,虽然集群搭建的文章在网上很多,我比较喜欢这篇文章的地方是 ...

  6. plsql中的光标

    操作oracle数据库效率最高的语言就是plsql程序,故而把访问数据库的代码写成plsql的执行效率要高于java,c ,c++等代码

  7. Perl 变量:哈希变量

    Perl 哈希变量哈希是 key/value 对的集合.Perl中哈希变量以百分号 (%) 标记开始.访问哈希元素格式:${key}. 1.创建哈希创建哈希可以通过以下两种方式: 1.为每个 key ...

  8. Windows 10 归档、对于一些问题的解决与软件推荐

    I'm a Windows Insider 最近加入了 Windows Insider 计划,主要目的还是为了体验一下马上(7.29)就要发售的 Windows 10 操作系统. 先简要介绍下 Win ...

  9. Python实现常见算法[3]——汉罗塔递归

    #!/usr/bin/python # define three list var. z1 = [1,2,3,4,5,6,7,"1st zhu"] z2 = ["2st ...

  10. cocoapods使用问题集锦(2017-04)

    今天公司在公司新发的电脑上边安装cocoapod发现容易忘记的几个问题,感觉需要记录下来. 问题一:系统默认ruby镜像的卸载命令行 -->     gem sources --remove h ...