zoukankan      html  css  js  c++  java
  • Android-自定义IntentSession来传递数据

    在上一篇博客中介绍到,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();

  • 相关阅读:
    20162324 2016-2017-2《Java程序设计》课程总结
    Java实验五网络编程与安全
    Java结对编程之挑战出题
    实验四Android开发
    Java四则运算总结
    实验三
    Java结对编程四则运算一周小结
    队列课下作业
    20162325 金立清 S2 W5 C14
    2017-2018-1 我爱学Java 第二周 作业
  • 原文地址:https://www.cnblogs.com/android-deli/p/10108803.html
Copyright © 2011-2022 走看看