zoukankan      html  css  js  c++  java
  • Android--在碎片中通过onSaveInstanceState()方法保存数据

    1. 在碎片中通过onSaveInstanceState()方法保存数据

    1)实现目标

    ·在rightfragment中输入临时数据;

    ·在模拟内存回收后,重新得到临时数据

    (2)模拟内存回收

    在模拟器下找到开发者设置,打开不保留活动

    *夜神模拟器中找到关于平板电脑选项连击版本型号,就可以到开发者设置了。

     

    (3)rightfragment中编写onSaveInstanceState()方法

    @Override
    public void onSaveInstanceState(Bundle outState) {
        String str = editText.getText().toString();
        outState.putString("right_content",str);
        super.onSaveInstanceState(outState);
    }

    获取editText中的临时数据,通过outState.putString(),函数将数据保存。

    (4)rightfragmentonCreatView()中实现数据打印

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.right_fragment,container,false);
        Log.d(TAG,"onCreateView");
        editText = (EditText) view.findViewById(R.id.edittext);
        if(savedInstanceState!=null){
            String str = savedInstanceState.getString("right_content");
            editText.setText(str);
            Log.d(TAG, str);
        }
        return view;
    }

    (5)结果显示

    Home键模拟内存回收

     

    再次返回app时,重新建立RightFragment并打印了临时数据

     

  • 相关阅读:
    树莓派镜像拷贝
    zookeeper客户端Watcher管理
    设置ZooKeeper服务器地址列表源码解析及扩展
    利用zookeeper实现发布订阅模式
    maven依赖错误排查经验
    理解CMS GC日志
    深入分析ThreadLocal
    Fail-Fast分析
    Stream学习过程中遇到的一个问题记录
    ClassLoader 学习笔记
  • 原文地址:https://www.cnblogs.com/ttnrt/p/11653653.html
Copyright © 2011-2022 走看看