`
bluewave
  • 浏览: 6533 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

onSaveInstanceState和onRestoreInstanceState的用处(转载)

阅读更多
很多不明白Activity类中包含的onSaveInstanceState和onRestoreInstanceState有什么用,首先声明下使用这两个方法时一定要注意情况和了解Activity的生命周期,否则有的时候  onSaveInstanceState和onRestoreInstanceState 可能不会被触发,虽然他们都是Activity的重写方法。(文/Android开发网)

他们比较常用到的地方是 Sensor、Land和Port布局的自动切换,过去Android开发网曾经说过解决横屏和竖屏切换带来的数据被置空或者说onCreate被重复调用问题,其实Android提供的onSaveInstanceState方法可以保存当前的窗口状态在即将布局切换前或当前Activity被推入历史栈,其实布局切换也调用过onPause所以被推入Activity的history stack,如果我们的Activity在后台没有因为运行内存吃紧被清理,则切换回时会触发onRestoreInstanceState方法。

这两个方法中参数均为Bundle,可以存放类似 SharedPreferences 的数据,所以使用它们作为当前窗口的状态保存是比较合适的。实际使用代码
@Override
  protected void onSaveInstanceState(Bundle outState){
            outState.putString("lastPath", "/sdcard/android123/cwj/test");
  }


@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);

String cwjString = savedInstanceState.getString("lastPath");
}
复制代码
分享到:
评论

相关推荐

    Android onSaveInstanceState和onRestoreInstanceState触发的时机

    Android onSaveInstanceState和onRestoreInstanceState触发的时机 Android 中的 onSaveInstanceState 和 onRestoreInstanceState 是两个重要的生命周期方法,它们在 Activity 的生命周期中扮演着关键角色。了解这...

    Android代码-自动帮你完成 onSaveInstanceState 和 onRestoreInstanceState 的相关操作

    No boilerplate code like onSaveInstanceState or onRestoreInstanceState any more. Getting started Just add the @AutoRestore annotation to your fields that need to be saved and restored in Activities, ...

    Android代码-svc

    "View" code and "Screen Code" such as onCreate,onCreateView, onViewCreated or onSaveInstanceState, onRestoreInstanceState.onActivityResult etc. It makes hard to see each "View Logic" and "Screen Logic...

    IceCreamOrder.zip

    简单的订单安卓程序,其中包括OnClickListener,TextWatcher,onSaveInstanceState,onRestoreInstanceState,onConfigurationChanged的用法

    onSaveInstanceState保存数据demo

    下面将详细解释`onSaveInstanceState()`的工作原理以及如何利用它来保存和恢复数据。 **1. onSaveInstanceState()的生命周期** 在Android中,Activity有四个主要的生命周期方法:`onCreate()`, `onStart()`, `...

    onSaveInstanceState调用的小例子

    然后,当活动被重新创建(如屏幕旋转后),Android会调用`onCreate`或`onRestoreInstanceState`,在这里你可以从`savedInstanceState`参数中恢复之前保存的状态: ```java @Override protected void onCreate...

    View的状态是如何保存的

    BaseSavedState是View的一个内部静态类,他实现了Parcelable接口,会把控件的属性(如selStart)序列化到Parcelable容器,这个内部类在View的onSaveInstanceState()方法里面以及onRestoreInstanceState(()方法里面...

    用ActivityGroup解决TabHost中多个Activity跳转问题

    onCreateView用于加载子Activity的布局,onDestroyView则用于清理资源,而onSaveInstanceState和onRestoreInstanceState则用于保存和恢复子Activity的状态。 2. **启动子Activity**:在ActivityGroup中,我们需要...

    井字游戏

    为了实现看起来像弹出窗口的活动,我必须获取显示器的宽度和高度并将其加载到DisplayMetric上,此外,我还尝试学习使用onSaveInstanceState和onRestoreInstanceState来保持游戏状态如果设备旋转。 面板的UI元素由九...

    android运用学习

    此外,活动的生命周期和保存状态的方法(如onSaveInstanceState和onRestoreInstanceState)也是重要知识点。 第9章可能涵盖“服务(Service)与广播接收器(Broadcast Receiver)”。服务是在后台运行,不与用户...

    安卓视频播放悬浮全屏竖屏切换技术

    这涉及到onSaveInstanceState和onRestoreInstanceState方法的使用。在onSaveInstanceState中,我们可以保存VideoView的当前播放位置和状态,如当前播放时间、是否正在播放等。在onRestoreInstanceState中,恢复这些...

    CountTime-AndroidStudio项目制作倒计时模块

    可以使用SharedPreferences存储当前倒计时值,或者在onSaveInstanceState和onRestoreInstanceState中处理。 7. **通知或振动提醒**: 当倒计时结束时,可以添加通知提醒用户,或者使用Vibrator类使设备振动,增加...

    Android 简单代码实现的Android 计算器源码.rar

    这可以通过保存和恢复实例状态(onSaveInstanceState和onRestoreInstanceState)或者使用ViewModel来实现。 在标签"android源码"的上下文中,这个计算器源码提供了一个实践Android开发基础的实例,包括UI设计、事件...

    俄罗斯方块

    这可以通过 onSaveInstanceState 和 onRestoreInstanceState 方法实现,或者使用ViewModel类来持久化数据。 然后,用户交互是游戏体验的关键。在《俄罗斯方块》中,用户可以左右移动方块、旋转方块或加速方块下落。...

    andriod各个知识点.pdf

    Android高级开发面试题及答案解析 ...通过对Activity启动流程和onSaveInstanceState()、onRestoreInstanceState()的调用时机的理解,我们可以更好地掌握Android开发的基础知识,并更好地应对面试中的难题。

    Basketball

    为保持数据不丢失,可以使用Intent的extras来保存状态,或者重写onSaveInstanceState和onRestoreInstanceState方法,将关键数据保存到Bundle中。 Kotlin的使用让代码更加简洁且类型安全。例如,它提供了空安全特性...

    ActivityandroidIntent详解.pdf

    Activity的状态保存和恢复是通过onSaveInstanceState和onRestoreInstanceState方法实现的。当Activity被销毁或由于系统原因需要暂时隐藏时,可以调用onSaveInstanceState方法来保存关键数据到Bundle对象中,然后在...

    popular-movies-part-two:Android应用程序可通过MovieDB API来显示热门电影或收视率最高的电影列表以及该电影的相应预告片和评论。 [Udacity]

    使用onSaveInstanceState / onRestoreInstanceState重新创建活动。 选择预告片后,应用程序会使用意图启动预告片。 保存旋转时细节活动滚动视图的滚动位置。 可选功能 存储电影海报,剧情简介,用户评分和发行...

    android 五子棋游戏

    此外,为了防止用户意外退出导致游戏丢失,可以利用Android的onSaveInstanceState和onRestoreInstanceState方法保存和恢复临时的游戏状态。 退出功能是通过结束当前Activity或者调用System.exit()方法实现,但后者...

Global site tag (gtag.js) - Google Analytics