Android实现在横竖屏切换时信息保留不会丢失

来源:互联网 发布:复制信息淘宝打不开 编辑:程序博客网 时间:2024/06/05 11:33

   一般在手机进行横竖屏切换的时候,由于Activity会被重置,从而导致信息丢失,比如正在编辑的文字没有了等。保留信息的方法目前据我所知有两种。


  一、在Activity被重置之前保留信息,在Activity再次被onCreate的时候将

         信息读取并加载

                     保留信息:

           @Override           protected void onSaveInstanceState(@NonNull Bundle outState) {           // If there's an animation in progress, cancel it first to ensure our state is up-to-date.            if (mCurrentAnimator != null) {               mCurrentAnimator.cancel();            }            super.onSaveInstanceState(outState);            outState.putInt(KEY_CURRENT_STATE, mCurrentState.ordinal());            outState.putString(KEY_CURRENT_EXPRESSION,                mTokenizer.getNormalizedExpression(mFormulaEditText.getText().toString()));           }

                    保留信息的代码:

          outState.putInt(KEY_CURRENT_STATE, mCurrentState.ordinal());          outState.putString(KEY_CURRENT_EXPRESSION,                mTokenizer.getNormalizedExpression(mFormulaEditText.getText().toString()));


                所以我们一般会重载 protected void onSaveInstanceState(@NonNull Bundle outState) 方法来保留信息。

                既然保留了,那么当然需要重新加载了,我们在onCreate里面进行如下:

        savedInstanceState = savedInstanceState == null ? Bundle.EMPTY : savedInstanceState;        setState(CalculatorState.values()[                savedInstanceState.getInt(KEY_CURRENT_STATE, CalculatorState.INPUT.ordinal())]);        mFormulaEditText.setText(mTokenizer.getLocalizedExpression(                savedInstanceState.getString(KEY_CURRENT_EXPRESSION, "")));

             这样就会将我们在onSaveInstanceState(@NonNull Bundle outState)里面保存的信息读取出来。


  二、配置onConfigurationChanged.

                   配置onConfigurationChanged,横竖屏切换的时候Activity不会被重置,当然信息也不会丢失。配置方法见:

                  http://m.blog.csdn.net/blog/dikevin0512/27357793


              还遗留有以下问题,哪位大神如果知道的话,麻烦交流下。
             1.我这边之前使用相对布局,切换到横屏的时候布局显示不对,目前不知道使用什么方法能够使布局能够正确显示。不过我换成线性布局就不会

                 存在这样的问题。有没有办法使得不管我使用什么布局,都能够正确显示呢?

              2.public void onConfigurationChanged(Configuration newConfig)这个函数一定要重载吗?我看到好多文件里面并没有重载这个函数,但是同时

                 也看到有几个文件里面加了这个函数,说是有bug,比如:

        ConversationList.java (e:\qualcomm\ap\packages\apps\mms\src\com\android\mms\ui)

      @Override      public void onConfigurationChanged(Configuration newConfig) {        // We override this method to avoid restarting the entire        // activity when the keyboard is opened (declared in        // AndroidManifest.xml).  Because the only translatable text        // in this activity is "New Message", which has the full width        // of phone to work with, localization shouldn't be a problem:        // no abbreviated alternate words should be needed even in        // 'wide' languages like German or Russian.        super.onConfigurationChanged(newConfig);        if (DEBUG) Log.v(TAG, "onConfigurationChanged: " + newConfig);    }

          看到这个里面就是不重载的话会有问题,那么这个函数到底必须重载还是在什么情况下需要重载呢?我尝试在我的代码里面重载和不重载感觉没有任何区别的。


               
















0 0
原创粉丝点击