• android activity状态保存


    一、被其他任务打断(来电话),再次打开希望保留数据

        private String SAVE_INSTANCE_TAG = "ATWAL";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_cal);
    
            if (savedInstanceState != null) {
                String s = savedInstanceState.getString(SAVE_INSTANCE_TAG);
            }
        }
    
        //系统在回收当前Activity之前会调用,Back & Home 不会调用此方法
        @Override
        public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
            if (outState != null) {
                outState.putString(SAVE_INSTANCE_TAG, "hello atwal");
            }
            super.onSaveInstanceState(outState, outPersistentState);
        }

    二、希望横竖屏切换、按了 Home or Back 键后再回来保留数据

        @Override
        protected void onResume() {
            super.onResume();
            String s = this.getPreferences(MODE_PRIVATE).getString(SAVE_INSTANCE_TAG, "");
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            this.getPreferences(MODE_PRIVATE).edit().putString(SAVE_INSTANCE_TAG, "after pause");
        }
  • 相关阅读:
    4.函数
    3.文件操作及编码解码补充
    2.列表字符串字典元组集合
    1.杂项三元运算及字符编码
    python-数据类型补充及文件处理操作
    python-day02数据类型-字符串和列表的操作
    python-day01
    DOM
    javascript基本
    CSS几个属性
  • 原文地址:https://www.cnblogs.com/lurenjiashuo/p/android-saveinstancestate.html
Copyright © 2020-2023  润新知