牛骨文教育服务平台(让学习变的简单)

当遇到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值
	@Override
	protected Parcelable onSaveInstanceState() {
		
		Bundle bundle=new Bundle();
		bundle.putParcelable(INSTANCE_STATUS, super.onSaveInstanceState());  //把父级存储的变量放到INSTANCE_STATUS中
		bundle.putFloat(STATUS_ALPHA, mAlpha);  //存储自己需要保存的东西
		
		return bundle;
	}
	
	@Override
	protected 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);
	}