super.onCreate(savedInstanceState);savedInstanceState的用法

1、super.onCreate()原因

在重写Activity声明周期回调方法时,必须先通过super调用父类的相关方法,对这个的官方解释如下:

/*
    Every Activity you make is started through a sequence of method calls. onCreate() is the first of these calls.
    Each and every one of your Activities extends android.app.Activity either directly or by subclassing another subclass of Activity.
    In Java, when you inherit from a class, you can override its methods to run your own code in them. A very common example of this is the overriding of the toString() method when extending /java.lang.Object.
    When we override a method, we have the option of completely replacing the method in our class, or of extending the existing parent class' method. By calling super.onCreate(savedInstanceState);, you tell the Dalvik VM to run your code in addition to the existing code in the onCreate() of the parent class. If you leave out this line, then only your code is run. The existing code is ignored completely.
    However, you must include this super call in your method, because if you don't then the onCreate() code in Activity is never run, and your app will run into all sorts of problem like having no Context assigned to the Activity (though you'll hit a SuperNotCalledException before you have a chance to figure out that you have no context).
    In short, Android's own classes can be incredibly complex. The code in the framework classes handles stuff like UI drawing, house cleaning and maintaining the Activity and application lifecycles. super calls allow developers to run this complex code behind the scenes, while still providing a good level of abstraction for our own apps.
*/

2、savedInstanceState用法

savedInstanceState的本质是一个bundle,可以以key-value的形式保存当前Activity的状态信息。这个参数作用是在游戏退出前,或者是电子书退出前,可以保存当前游戏或电子书的运行状态或页码,保证在下次启动Activity的时候可以继续。可以通过重写

@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putInt("key1", 0);
        savedInstanceState.putString("key2", "savedInstanceState test");
        Log.e(TAG, "onSaveInstanceState");
    }

来保存信息,回调顺序如下:

2022-04-25 18:22:34.567 18451-18451/com.example.uitest E/MainActivity: onPause
2022-04-25 18:22:34.567 18451-18451/com.example.uitest E/MainActivity: onSaveInstanceState
2022-04-25 18:22:35.324 18451-18451/com.example.uitest E/MainActivity: onStop
2022-04-25 18:22:35.325 18451-18451/com.example.uitest E/MainActivity: onSaveInstanceState
2022-04-25 18:22:35.352 18451-18451/com.example.uitest E/MainActivity: onDestroy

通过重写

@Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        int IntTest = savedInstanceState.getInt("key1");
        String StrTest = savedInstanceState.getString("key2");
        Log.e(TAG, "onRestoreInstanceState");
    }

来获取状态,当然,在onCreate方法里也可以获取这个参数状态。方法回调顺序是:

2022-04-25 17:26:13.095 18451-18451/com.example.uitest E/MainActivity: onCreate
2022-04-25 17:26:13.108 18451-18451/com.example.uitest E/MainActivity: savedInstanceState 
2022-04-25 17:26:13.111 18451-18451/com.example.uitest E/MainActivity: onStart
2022-04-25 17:26:13.111 18451-18451/com.example.uitest E/MainActivity: onRestoreInstanceState
2022-04-25 17:26:13.112 18451-18451/com.example.uitest E/MainActivity: onResume

这里有个问题,我一开始写的时候一直不能正常存储再读出bundle里的值,是因为正常的退出不会回调这个流程,会走onRestart流程,只有Activity在低内存或其他情况切到后台被退掉才会走onRestoreInstanceState这个流程。如果用模拟器验证可以开启开发者选项,开启不保留活动或者用第三方工具一键清理。开启不保留活动后,打开Activity,按返回键退出也不会保存,经过测试在以下几种情况下可以:

1、用户按下home键

2、用户唤起历史任务,并选择某个任务(其他app)

3、用户按下电源键

4、屏幕方向切换

5、在不finish的前提下,ActivityA启动ActivityB

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值