[英]onCreate() of Launcher activity gets called when app launched on clicking launcher icon
[英]How do I create a root launcher Activity in an app, when onCreate() is not called on Android 12, API 31?
我想創建一個具有特定 Activity 的 Android 應用程序,它充當'Root Launcher Activity' ,有時在應用程序中啟動其他活動。 當這個 Root Launcher Activity 啟動時,它會檢查一個首選項,看它是否可以直接啟動不同的 Activity。
多年來,在 Android 11 (API 30) 及更低版本上運行良好:
登錄活動.kt
/** This is the main entry point of the app, when the user clicks the app icon. */
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// If the user is already logged in, don't show the Login screen.
// Instead proceed directly to the Welcome screen.
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
val userIsLoggedIn = prefs.getBoolean("USER_IS_LOGGED_IN", false)
if (userIsLoggedIn) {
launchWelcomeScreenActivity()
finish() // Close LoginActivity
} else {
displayLoginFields()
}
}
override fun onResume() {
super.onResume()
Log.d("MyApp", "onResume()")
// Nothing to do here
}
...
}
AndroidManifest.xml
...
<application
android:icon="@drawable/app_icon">
<activity
android:name=".LoginActivity"
android:exported="true">
<!-- This Activity is the main entry point to the app. Create an app icon for it. -->
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
...
</application>
...
現在假設用戶單擊WelcomeScreenActivity
上的“注銷”按鈕。 它應該將用戶注銷,但仍留在該屏幕上並顯示圖像:
class WelcomeScreenActivity : AppCompatActivity() {
fun onLogoutClicked() {
// Clear the logged in state and display an image
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
prefs.edit().putBoolean("USER_IS_LOGGED_IN", false).apply()
showLoggedOutImage()
}
}
當用戶按下后退按鈕時,他們將退出應用程序,因為WelcomeScreenActivity
是堆棧中唯一剩下的一個。
但現在問題出現在 Android 12 及更高版本上:當用戶通過單擊應用圖標重新啟動應用時,它將啟動LoginActivity.onResume()
並顯示錯誤的屏幕。 這是因為LoginActivity.onCreate()
在這種情況下不再被調用,由於Activity 生命周期行為的變化,對於 Android 12/13 ,引用如下:
根啟動器活動不再在后按時完成。
Android 12 更改了系統對位於其任務根目錄的啟動器活動的后按的默認處理。 在以前的版本中,系統將在 Back press 上完成這些活動。 在 Android 12 中,系統現在將 Activity 及其任務移至后台,而不是完成 Activity。 使用 Home 按鈕或手勢離開應用程序時,新行為與當前行為相匹配。
我想要和期望的是它應該啟動LoginActivity.onCreate()
並調用displayLoginFields()
,就像在 Android 11 及更低版本上一樣。 在 Android 12 上,沒有明顯的方法可以在后退按鈕退出后讓LoginActivity
狀態重新啟動。
那么處理這個邏輯的正確方法是什么,讓 root 啟動器可以在所有 Android 操作系統版本上運行呢?
onCreate()
移動到onResume()
還是onNewIntent()
? 這會有什么副作用嗎?onResume()
中檢查應用程序是否在 Android 12 上運行,並為其使用特定行為,如下所示:/** This is the main entry point of the app, when the user clicks the app icon. */
class LoginActivity : AppCompatActivity() {
...
override fun onResume() {
super.onResume()
val ANDROID_12_API_31 = 31
if (Build.VERSION.SDK_INT >= ANDROID_12_API_31) {
// Add the Activity launcher code from onCreate()
}
}
...
}
LoginActivity.onCreate()
?類似/相關的問題,但不完全相同的問題: 如何設置導航堆棧的根
請注意,Android 12 於 2021 年 10 月發布。
為什么不直接覆蓋onBackPressed()
以便它完成Activity
而不是調用super.onBackPressed()
? 然后將恢復舊的后退按鈕行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.