用onSaveInstanceState()方法保存Activity状态
Posted 君子求诸己
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用onSaveInstanceState()方法保存Activity状态相关的知识,希望对你有一定的参考价值。
https://blog.csdn.net/yuzhiboyi/article/details/7677026
实现并覆盖了onSaveInstanceState方法:
- @Override
- protected void onSaveInstanceState(Bundle outState) {
- // TODO Auto-generated method stub
- outState.putInt("currentposition", videoView.getCurrentPosition());
- Log.v("tag", "onSaveInstanceState");
- super.onSaveInstanceState(outState);
- }
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Log.v("tag", "onCreate");
- if (videoView == null) {
- videoView = (VideoView) this.findViewById(R.id.myvideo);
- MediaController controller = new MediaController(this);
- videoView.setMediaController(controller);
- videoView.setVideoPath(VIDEO_PATH);
- videoView.requestFocus();
- }
- if (savedInstanceState != null
- && savedInstanceState.getInt("currentposition") != 0) {
- videoView.seekTo(savedInstanceState.getInt("currentposition"));
- }
- videoView.start();
- }
onCreate方法中的参数savedInstanceState就是保存的Activity一些状态。
- savedInstanceState.getInt("currentposition")
获取视频播放时间。
红色代码是将当前video的播放时间存储在Bundle中。
以上是关于用onSaveInstanceState()方法保存Activity状态的主要内容,如果未能解决你的问题,请参考以下文章
android源码解析(二十四)-->onSaveInstanceState执行时机
onSaveInstanceState() 和 onRestoreInstanceState()
Android onSaveInstanceState()恢复数据
Android-onSaveInstanceState方法啥时候被调用
“在 onSaveInstanceState 之后无法执行此操作” - 为啥我会从我的活动的 onResume 方法中收到此异常?
IllegalStateException: Can not perform this action after onSaveInstanceState 错误解决方法