关于Activity回收造成View选中不对应的问题

来源:互联网 发布:淘宝男士内衣 编辑:程序博客网 时间:2024/04/28 15:33


       当遇到Activity被回收(横竖屏、内存不足)时,Activity会重建,而去调用onCreate()方法,在onCreate()方法中调用了设置首项透明度的方法。这样就会出现,选中的View和内容Fragment的不对应的。

//Bundle的键,作用:自定义的VIew继承的有可能不是View,有可能是TextView、ImageView,//重写下面两个方法,以便记住原本的Bundle(不能抹掉原来的XXView的恢复和销毁的过程)private static final String INSTANCE_STATUS="instance_status";  private static final String STATUS_ALPHA="status_alpha";  //Bundle的键//当Activity重建的时候,恢复Alpha值@Overrideprotected Parcelable onSaveInstanceState() {Bundle bundle=new Bundle();bundle.putParcelable(INSTANCE_STATUS, super.onSaveInstanceState());  //把父级存储的变量放到INSTANCE_STATUS中bundle.putFloat(STATUS_ALPHA, mAlpha);  //存储自己需要保存的东西return bundle;}@Overrideprotected void onRestoreInstanceState(Parcelable state) {if(state instanceof Bundle){Bundle bundle=(Bundle) state;mAlpha=bundle.getFloat(STATUS_ALPHA);   //取出自己保存的东西super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATUS));  //取出系统保存的东西,并调用系统的恢复return;}super.onRestoreInstanceState(state);}



0 0