在上一篇博客中介绍到,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. linux grep日志查询

    ll access.2018-09-*.gz zcat access.2018-09-*.gz |grep --color '1073011900' | head -n  100  匹配字符由于管道h ...

  2. numpy的通用函数

    通用函数:快速的元素级数组函数 通用函数是一种对ndarry中的数据执行元素级运算的函数,可以看作是简单函数(接受一个或多个标量值,并产生一个或多个标量值)的矢量化包装器. 一元func: abs丶f ...

  3. 「小程序JAVA实战」springboot的后台搭建(31)

    转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootdehoutaidajian31/ 根据下面的图,我们来建立下对应的sp ...

  4. eclipse中的实用快捷键

    之前有写过“myeclipse实用快捷键”,今天总结一下“eclipse中的快捷键”. 1.打开文件Crtl+Shift+R: 2. 打开类文件包括能看到字在哪个jar   Ctrl+Shift+T: ...

  5. fastjson集合转字符串

    JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect); list为集合

  6. 【原】Coursera—Andrew Ng机器学习—Week 7 习题—支持向量机SVM

    [1] [2] Answer: B. 即 x1=3这条垂直线. [3] Answer: B 因为要尽可能小.对B,右侧红叉,有1/2 * 2  = 1 ≥ 1,左侧圆圈,有1/2 * -2  = -1 ...

  7. struts2 action重定向action中文乱码处理

    比如:Action方法productCategorySave()变量message,传递给Action方法productCategoryAdd(),当变量message为中文变量时,要进行编码设置,不 ...

  8. Django框架开发web网站的网页优化—页面静态化

    网站优化-页面静态化 1)概念 提前将页面所用到的数据从数据库查询出来,然后生成一个静态页面,之后用户来访问的时候,直接返回静态页面. 举例:首页静态化:获取首页用到的数据表中的数据,生成静态首页in ...

  9. mysql存储过程(procedure)

    #创建带参数的存储过程 delimiter // ),out p int) begin ; end // delimiter call pro_stu_name_pass(@n,@p); select ...

  10. Linux实战教学笔记46:NoSQL数据库之redis持久化存储 (二)

    第3章 Redis数据类型详解 3.1 Redis键/值介绍 Redis key值是二进制安全的,这意味着可以用任何二进制序列作为key值,从形如"foo"的简单字符串到一个JPG ...