Android basic notes: Activity life cycle onCreate () the difference between one parameter and two parameters

Activity life cycle onCreate () the difference between one parameter and two parameters

	@Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.i(TAG, "1、调用 onCreate(Bundle savedInstanceState) 方法,首次创建 Activity ");
    }
 	@Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    
    
        super.onCreate(savedInstanceState, persistentState);

        Log.i(TAG,"调用 onCreate(Bundle savedInstanceState, PersistableBundle persistentState)");
    }

onCreate(Bundle savedInstanceState, PersistableBundle persistentState) is a new method provided by 5.0. Before rewriting, you must set an attribute for the Activity in the configuration file AndroidManifest.xml: android:persistableMode="persistAcrossReboots", so that the Activity has persistence Ability

	<activity
            android:name=".MainActivity"
            android:persistableMode="persistAcrossReboots">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

onCreate(Bundle savedInstanceState, PersistableBundle persistentState) will be used with two other methods:


When overriding the two-parameter onCreate() method and two collocation methods, it is found that the modifiers of the method are all public, while the modifiers of one parameter and several lifecycle methods are protected.


	 /**onSaveInstanceState 使用情形
     * 1、点击home键回到主页或长按后选择运行其他程序
     * 2、按下电源键关闭屏幕
     * 3、启动新的 Activity
     * 4、横竖屏切换时,肯定会执行,因为横竖屏切换的时候会先销毁 Activity,
     *    然后再重新创建 重要原则:当系统"未经你许可"时销毁了你的 Activity,
     *    则onSaveInstanceState会被系统调用, 这是系统的责任,
     *    因为它必须要提供一个机会让你保存你的数据(你可以保存也可以不保存但是必须提供!)。
     */

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle persistentState) {
    
    
        super.onSaveInstanceState(outState);

        Log.i(TAG, "调用 onSaveInstanceState(Bundle outState, PersistableBundle persistentState) 方法");
    }


    /**onRestoreInstanceState
     *和onCreate同样可以从取出前者保存的数据:
     * 一般是在onStart()和onResume()之间执行!
     * 之所以有两个可以获取到保存数据的方法,是为了避免 Activity 跳转而没有关闭,
     * 然后不走onCreate()方法,而你又想取出保存数据
     */
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
    
    
        super.onRestoreInstanceState(savedInstanceState, persistentState);

        Log.i(TAG,"调用 onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState)");
    }

Activity has the ability to persist. The added PersistableBundle parameter enables these methods to have the ability to restore data after the system is shut down, and does not affect our other serialization operations.

运行程序:
MainActivity: 1、调用 onCreate(Bundle savedInstanceState) 方法,首次创建 Activity 
MainActivity: 2、调用 onStart() 方法,将 Activity 显示给用户
MainActivity: 3、调用 onResume() 方法,使 Activity 位于前台;此时Activity 位于栈顶
OpenGLRenderer: Initialized EGL, version 1.4
按下home键:
MainActivity: 执行了 onPause() 方法
MainActivity: 执行了 onStop() 方法
打开程序:
MainActivity: 执行了 onRestart() 方法
MainActivity: 2、调用 onStart() 方法,将 Activity 显示给用户
MainActivity: 3、调用 onResume() 方法,使 Activity 位于前台;此时Activity 位于栈顶
退出程序:
MainActivity: 执行了 onPause() 方法
MainActivity: 执行了 onStop() 方法
MainActivity: 执行了 onDestroy() 方法
点击程序:
MainActivity: 1、调用 onCreate(Bundle savedInstanceState) 方法,首次创建 Activity 
MainActivity: 调用 onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
MainActivity: 2、调用 onStart() 方法,将 Activity 显示给用户
MainActivity: 3、调用 onResume() 方法,使 Activity 位于前台;此时Activity 位于栈顶
退出程序:
MainActivity: 执行了 onPause() 方法
MainActivity: 执行了 onStop() 方法
MainActivity: 执行了 onDestroy() 方法

Insert picture description here

Guess you like

Origin blog.csdn.net/drawababy/article/details/113681404