Android 中 onSaveInstanceState()使用方法详解

时间:2022-07-28
本文章向大家介绍Android 中 onSaveInstanceState()使用方法详解,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Android 中 onSaveInstanceState()使用方法详解

覆盖onSaveInstanceState方法,并在onCreate中检测savedInstanceState和获取保存的值

@Override 
protected void onSaveInstanceState(Bundle outState) {  
  outState.putInt("currentposition", videoView.getCurrentPosition()); 
  super.onSaveInstanceState(outState); 
} 
public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main);  

  initView(); 

  if (savedInstanceState != null 
      && savedInstanceState.getInt("currentposition") != 0) { 

    videoView.seekTo(savedInstanceState.getInt("currentposition")); 
  } 
} 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!