用onSaveInstanceState()方法保存Activity狀態
阿新 • • 發佈:2018-06-07
super pub bundle .net blank https views () tst
https://blog.csdn.net/yuzhiboyi/article/details/7677026
實現並覆蓋了onSaveInstanceState方法:
[java] view plaincopy
- @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一些狀態。
[java] view plaincopy
- savedInstanceState.getInt("currentposition")
獲取視頻播放時間。
紅色代碼是將當前video的播放時間存儲在Bundle中。
用onSaveInstanceState()方法保存Activity狀態