onSaveInstanceState() 与 fragment.commit()

首先谈一下,onSaveInstanceState()

Activity里的onSaveInstanceState()方法,虽然系统会自动调用它来保存Activity的一些数据,但当除它默认要保存的数据外,我们还要保存一些其他数据的时候, 我们就需要覆盖onSaveInstanceState()方法来保存Activity的附件信息。例如在播放视频过程中,横竖屏切换要保持当前播放时间进度,在默认情况下播放时间是不被自动保存的。

写了一个简单的播放视频的例子,在横竖屏切换时保持当前播放进度,

mian.xml的代码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.      
  6.     <VideoView android:id="@+id/myvideo"   android:layout_width="wrap_content"  
  7.               android:layout_height="wrap_content" />  
  8. </LinearLayout>  

MainAcrtivity主要代码部分:

  1. private VideoView videoView;  
  2. private static final String VIDEO_PATH = Environment  
  3.         .getExternalStorageDirectory()  
  4.         + File.separator  
  5.         + "mymovie"  
  6.         + File.separator + "shenghuaweiji.mp4";  
  7.   
  8. /** Called when the activity is first created. */  
  9. @Override  
  10. public void onCreate(Bundle savedInstanceState) {  
  11.     super.onCreate(savedInstanceState);  
  12.     setContentView(R.layout.main);  
  13.     Log.v("tag""onCreate");  
  14.      
  15.     if (videoView == null) {  
  16.         videoView = (VideoView) this.findViewById(R.id.myvideo);  
  17.         MediaController controller = new MediaController(this);  
  18.         videoView.setMediaController(controller);  
  19.         videoView.setVideoPath(VIDEO_PATH);  
  20.         videoView.requestFocus();  
  21.     }  
  22.   
  23.     if (savedInstanceState != null  
  24.             && savedInstanceState.getInt("currentposition") != 0) {  
  25.   
  26.         videoView.seekTo(savedInstanceState.getInt("currentposition"));  
  27.     }  
  28.     videoView.start();  
  29.   
  30. }  

onCreate方法中的参数savedInstanceState就是保存的Activity一些状态。

  1. savedInstanceState.getInt("currentposition")  

获取视频播放时间。

实现并覆盖了onSaveInstanceState方法:

  1. @Override  
  2. protected void onSaveInstanceState(Bundle outState) {  
  3.     // TODO Auto-generated method stub  
  4.     outState.putInt("currentposition", videoView.getCurrentPosition());  
  5.     Log.v("tag""onSaveInstanceState");  
  6.     super.onSaveInstanceState(outState);  
  7. }   

红色代码是将当前video的播放时间存储在Bundle中。

这样在横竖屏切换时保证了播放状态,源代码:http://bigcateasymorse.googlecode.com/svn/trunk/save-activity-state1.0/


在谈一下 onSaveInstanceState() 与 fragment.commit()的关系

说着个问题,是因为在解bug中遇到monkey问题

IllegalStateException: Can not perform this action after onSaveInstanceState的解决方法 - 那...

使用Fragment的时候,出现了这个错误  IllegalStateException: Can not perform this action after onSaveInstanceState:如图

在使用FragmentTransition的 commit方法添加一个Fragment的时候出现的,那么原因是什么呢?

原因是 commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,因为 onSaveInstanceState方法是在该Activity即将被 销毁前调用,

来保存Activity数据的,如果在保存玩状态后再给它添加Fragment就会出错。 解决办法就是把commit()方法替换成 commitAllowingStateLoss()就行

了,其功能效果是一样的。

所以解决办法明朗!

转自:http://www.tuicool.com/articles/ziA3Mb




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值