【Tech-Android-Other】Android中保存界面状态

来源:互联网 发布:省 市 乡镇数据库设计 编辑:程序博客网 时间:2024/05/21 06:54
场景:一个Tab中的一个子Activty里有listView
       a.如果在此Activty中启动其他应用,当返回时系统为我门保存了当前状态(应该也是调用了以下方法来保存)。

       b.如果是在其他子Activty中启动其他应用,当返回时ListView滑动状态就会丢失。


       View类有一个继承自AbsSavedState的BaseSavedState类,所有View的子类都会继承这个BaseSavedState类。当然包括ListView,TextView等等。以下代码即可实现场景b中保存ListView滑动状态:

1.写出以下方法

[java]view plaincopyprint?
  1.    /** 
  2.     * Used to keep track of the scroll state of the list. 
  3.     */  
  4.    private Parcelable mListState = null;  
  5. private static final String LIST_STATE_KEY = "liststate";  
  6.    @Override  
  7.    protected void onSaveInstanceState(Bundle icicle) {  
  8.        super.onSaveInstanceState(icicle);  
  9.        // Save list state in the bundle so we can restore it after the QueryHandler has run  
  10.        if (mList != null) {  
  11.            icicle.putParcelable(LIST_STATE_KEY, mList.onSaveInstanceState());  
  12.        }  
  13.    }  
  14.   
  15.    @Override  
  16.    protected void onRestoreInstanceState(Bundle icicle) {  
  17.        super.onRestoreInstanceState(icicle);  
  18.        // Retrieve list state. This will be applied after the QueryHandler has run  
  19.        mListState = icicle.getParcelable(LIST_STATE_KEY);  
  20.    }  
2.在合适的地方调用(比如onCreate):

[java]view plaincopyprint?
  1. if(mListState!=null){  
  2.           mList.onRestoreInstanceState(mListState);  
  3.           mListState = null;  
  4. }  

         在ListView中SavedState继承自BaseSavedState,ListView中的实现如下:

[java]view plaincopyprint?
  1.     static class SavedState extends BaseSavedState {  
  2.         SparseBooleanArray checkState;  
  3.         LongSparseArray<Boolean> checkIdState;  
  4.   
  5.         /** 
  6.          * Constructor called from {@link ListView#onSaveInstanceState()} 
  7.          */  
  8.         SavedState(Parcelable superState, SparseBooleanArray checkState,  
  9.                 LongSparseArray<Boolean> checkIdState) {  
  10.             super(superState);  
  11.             this.checkState = checkState;  
  12.             this.checkIdState = checkIdState;  
  13.         }  
  14.   
  15.         /** 
  16.          * Constructor called from {@link #CREATOR} 
  17.          */  
  18.         private SavedState(Parcel in) {  
  19.             super(in);  
  20.             checkState = in.readSparseBooleanArray();  
  21.             long[] idState = in.createLongArray();  
  22.   
  23.             if (idState.length > 0) {  
  24.                 checkIdState = new LongSparseArray<Boolean>();  
  25.                 checkIdState.setValues(idState, Boolean.TRUE);  
  26.             }  
  27.         }  
  28.   
  29.         @Override  
  30.         public void writeToParcel(Parcel out, int flags) {  
  31.             super.writeToParcel(out, flags);  
  32.             out.writeSparseBooleanArray(checkState);  
  33.             out.writeLongArray(checkIdState != null ? checkIdState.getKeys() : new long[0]);  
  34.         }  
  35.   
  36.         @Override  
  37.         public String toString() {  
  38.             return "ListView.SavedState{"  
  39.                     + Integer.toHexString(System.identityHashCode(this))  
  40.                     + " checkState=" + checkState + "}";  
  41.         }  
  42.   
  43.         public static final Parcelable.Creator<SavedState> CREATOR  
  44.                 = new Parcelable.Creator<SavedState>() {  
  45.             public SavedState createFromParcel(Parcel in) {  
  46.                 return new SavedState(in);  
  47.             }  
  48.   
  49.             public SavedState[] newArray(int size) {  
  50.                 return new SavedState[size];  
  51.             }  
  52.         };  
  53.     }  
  54.   
  55. //以下实现状态的保存和回复  
  56.    @Override  
  57.     public Parcelable onSaveInstanceState() {  
  58.         Parcelable superState = super.onSaveInstanceState();  
  59.         return new SavedState(superState, mCheckStates, mCheckedIdStates);  
  60.     }  
  61.   
  62.     @Override  
  63.     public void onRestoreInstanceState(Parcelable state) {  
  64.         SavedState ss = (SavedState) state;  
  65.   
  66.         super.onRestoreInstanceState(ss.getSuperState());  
  67.   
  68.         if (ss.checkState != null) {  
  69.            mCheckStates = ss.checkState;  
  70.         }  
  71.   
  72.         if (ss.checkIdState != null) {  
  73.             mCheckedIdStates = ss.checkIdState;  
  74.         }  
  75.     }  

          View中,BaseSaveState的实现如下:

[java]view plaincopyprint?
  1. /** 
  2.  * Base class for derived classes that want to save and restore their own 
  3.  * state in {@link android.view.View#onSaveInstanceState()}. 
  4.  */  
  5. public static class BaseSavedState extends AbsSavedState {  
  6.     /** 
  7.      * Constructor used when reading from a parcel. Reads the state of the superclass. 
  8.      * 
  9.      * @param source 
  10.      */  
  11.     public BaseSavedState(Parcel source) {  
  12.         super(source);  
  13.     }  
  14.   
  15.     /** 
  16.      * Constructor called by derived classes when creating their SavedState objects 
  17.      * 
  18.      * @param superState The state of the superclass of this view 
  19.      */  
  20.     public BaseSavedState(Parcelable superState) {  
  21.         super(superState);  
  22.     }  
  23.   
  24.     public static final Parcelable.Creator<BaseSavedState> CREATOR =  
  25.             new Parcelable.Creator<BaseSavedState>() {  
  26.         public BaseSavedState createFromParcel(Parcel in) {  
  27.             return new BaseSavedState(in);  
  28.         }  
  29.   
  30.         public BaseSavedState[] newArray(int size) {  
  31.             return new BaseSavedState[size];  
  32.         }  
  33.     };  
  34. }  
            

         AbsSavedState类实现了Parcelable接口,具体实现如下:

[java]view plaincopyprint?
  1. /** 
  2.  * A {@link Parcelable} implementation that should be used by inheritance 
  3.  * hierarchies to ensure the state of all classes along the chain is saved. 
  4.  */  
  5. public abstract class AbsSavedState implements Parcelable {  
  6.     public static final AbsSavedState EMPTY_STATE = new AbsSavedState() {};  
  7.   
  8.     private final Parcelable mSuperState;  
  9.   
  10.     /** 
  11.      * Constructor used to make the EMPTY_STATE singleton 
  12.      */  
  13.     private AbsSavedState() {  
  14.         mSuperState = null;  
  15.     }  
  16.   
  17.     /** 
  18.      * Constructor called by derived classes when creating their SavedState objects 
  19.      *  
  20.      * @param superState The state of the superclass of this view 
  21.      */  
  22.     protected AbsSavedState(Parcelable superState) {  
  23.         if (superState == null) {  
  24.             throw new IllegalArgumentException("superState must not be null");  
  25.         }  
  26.         mSuperState = superState != EMPTY_STATE ? superState : null;  
  27.     }  
  28.   
  29.     /** 
  30.      * Constructor used when reading from a parcel. Reads the state of the superclass. 
  31.      *  
  32.      * @param source 
  33.      */  
  34.     protected AbsSavedState(Parcel source) {  
  35.         // FIXME need class loader  
  36.         Parcelable superState = source.readParcelable(null);  
  37.            
  38.         mSuperState = superState != null ? superState : EMPTY_STATE;  
  39.     }  
  40.   
  41.     final public Parcelable getSuperState() {  
  42.         return mSuperState;  
  43.     }  
  44.   
  45.     public int describeContents() {  
  46.         return 0;  
  47.     }  
  48.   
  49.     public void writeToParcel(Parcel dest, int flags) {  
  50.          dest.writeParcelable(mSuperState, flags);  
  51.     }  
  52.   
  53.     public static final Parcelable.Creator<AbsSavedState> CREATOR   
  54.         = new Parcelable.Creator<AbsSavedState>() {  
  55.           
  56.         public AbsSavedState createFromParcel(Parcel in) {  
  57.             Parcelable superState = in.readParcelable(null);  
  58.             if (superState != null) {  
  59.                 throw new IllegalStateException("superState must be null");  
  60.             }  
  61.             return EMPTY_STATE;  
  62.         }  
  63.   
  64.         public AbsSavedState[] newArray(int size) {  
  65.             return new AbsSavedState[size];  
  66.         }  
  67.     };  
  68. }  


原创粉丝点击