测试onNewIntent、onSaveInstanceState调用的例子

package com.ivan.activitylifetest

import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.widget.Button
import androidx.annotation.NonNull
import androidx.appcompat.app.AppCompatActivity

/**
 * android:launchMode="singleTask"
 */
class MainActivity : AppCompatActivity() {
    companion object {
        const val TAG = "MainActivity"
    }
    private var mCount = 10
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.e(TAG,"onCreate")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mCount = savedInstanceState?.getInt("count") ?: 100
        val btn_test = findViewById



    



    
        
            
                

                
            
        
    

1、MainActivity配置了android:launchMode="singleTask",再启动MainActivity,看到的日志是

2023-01-12 16:59:05.426 5522-5522/com.ivan.activitylifetest E/MainActivity: onPause
2023-01-12 16:59:05.426 5522-5522/com.ivan.activitylifetest E/MainActivity: onNewIntent,count=200
2023-01-12 16:59:05.429 5522-5522/com.ivan.activitylifetest E/MainActivity: onResume

2、旋转手机屏幕看到的日志是

2023-01-12 17:00:13.489 5522-5522/com.ivan.activitylifetest E/MainActivity: onPause
2023-01-12 17:00:13.493 5522-5522/com.ivan.activitylifetest E/MainActivity: onSaveInstanceState
2023-01-12 17:00:13.496 5522-5522/com.ivan.activitylifetest E/MainActivity: onStop
2023-01-12 17:00:13.500 5522-5522/com.ivan.activitylifetest E/MainActivity: onDestroy
2023-01-12 17:00:13.548 5522-5522/com.ivan.activitylifetest E/MainActivity: onCreate
2023-01-12 17:00:13.653 5522-5522/com.ivan.activitylifetest E/MainActivity: onStart
2023-01-12 17:00:13.664 5522-5522/com.ivan.activitylifetest E/MainActivity: onResume

可以在onCreate中获取到onSaveInstanceState保存的值

3、如果MainActivity配置了android:configChanges="orientation|screenSize|keyboardHidden",旋转手机屏幕没有日志打印

你可能感兴趣的:(Android,android)