1. 程式人生 > >用onSaveInstanceState()方法保存Activity狀態

用onSaveInstanceState()方法保存Activity狀態

super pub bundle .net blank https views () tst

https://blog.csdn.net/yuzhiboyi/article/details/7677026

實現並覆蓋了onSaveInstanceState方法:

[java] view plaincopy
  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. }
  1. /** Called when the activity is first created. */
  2. @Override
  3. public void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.main);
  6. Log.v("tag", "onCreate");
  7. if (videoView == null) {
  8. videoView = (VideoView) this.findViewById(R.id.myvideo);
  9. MediaController controller = new MediaController(this);
  10. videoView.setMediaController(controller);
  11. videoView.setVideoPath(VIDEO_PATH);
  12. videoView.requestFocus();
  13. }
  14. if (savedInstanceState != null
  15. && savedInstanceState.getInt("currentposition") != 0) {
  16. videoView.seekTo(savedInstanceState.getInt("currentposition"));
  17. }
  18. videoView.start();
  19. }

onCreate方法中的參數savedInstanceState就是保存的Activity一些狀態。

[java] view plaincopy
  1. savedInstanceState.getInt("currentposition")

獲取視頻播放時間。

紅色代碼是將當前video的播放時間存儲在Bundle中。

用onSaveInstanceState()方法保存Activity狀態