重新创建 activity 后 super.onCreate(savedInstanceState) 崩溃
Crash on super.onCreate(savedInstanceState) after recreate activity
我的项目中有两个活动 ActivityOne
和 MainActivity
。
我使用 ActivityOne
中的意图开始 MainActivity
,然后我在 MainActivity
中切换暗模式开关并在 activity 中调用 recreate()
。
但是 MainActivity
在重新创建时在 super.onCreate(savedInstanceState)
行崩溃。
androidx片段版本:1.2.5
生命周期版本:2.2.0
主要活动:
override fun onCreate(savedInstanceState: Bundle?) {
val userInterfaceMode = when (UIMode.currentUIMode) {
is UIMode.DarkMode -> AppCompatDelegate.MODE_NIGHT_YES
is UIMode.LightMode -> AppCompatDelegate.MODE_NIGHT_NO
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
AppCompatDelegate.setDefaultNightMode(userInterfaceMode)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
overridePendingTransition(
R.anim.enter_activity_fade_in_anim,
R.anim.enter_activity_fade_out_anim
)
if (savedInstanceState == null){
setupNavigation()
}
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
setupNavigation()
}
private fun setupNavigation(){
if (supportFragmentManager.findFragmentByTag(NAV_HOST_TAG) != null){
val host = supportFragmentManager
.findFragmentByTag(NAV_HOST_TAG) as NavHostFragment
supportFragmentManager
.beginTransaction()
.attach(host)
.setPrimaryNavigationFragment(host)
.commit()
}else{
val host = NavHostFragment.create(R.navigation.navigation_graph)
supportFragmentManager
.beginTransaction()
.add(
R.id.mainActivityRoot, host,
NAV_HOST_TAG
)
.setPrimaryNavigationFragment(host)
.commit()
}
}
Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.pitok.goodnews, PID: 6173
java.lang.RuntimeException: Unable to start activity ComponentInfo{MainActivity}: java.lang.IllegalArgumentException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4886)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4795)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:55)
at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:4845)
at android.app.ActivityThread.access00(ActivityThread.java:201)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1849)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.IllegalArgumentException
at androidx.lifecycle.LifecycleRegistry.downEvent(LifecycleRegistry.java:263)
at androidx.lifecycle.LifecycleRegistry.backwardPass(LifecycleRegistry.java:314)
at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:334)
at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
at androidx.lifecycle.LifecycleRegistry.setCurrentState(LifecycleRegistry.java:118)
at androidx.navigation.NavBackStackEntry.updateState(NavBackStackEntry.java:150)
at androidx.navigation.NavBackStackEntry.setMaxLifecycle(NavBackStackEntry.java:130)
at androidx.navigation.NavController.popBackStackInternal(NavController.java:325)
at androidx.navigation.NavController.dispatchOnDestinationChanged(NavController.java:426)
at androidx.navigation.NavController.onGraphCreated(NavController.java:636)
at androidx.navigation.NavController.setGraph(NavController.java:586)
at androidx.navigation.NavController.setGraph(NavController.java:551)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:247)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2685)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1368)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1446)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1509)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2637)
at androidx.fragment.app.FragmentManager.dispatchCreate(FragmentManager.java:2583)
at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:236)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:315)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:115)
at me.pitok.goodnews.appMain.MainActivity.onCreate(MainActivity.kt:24)
at android.app.Activity.performCreate(Activity.java:7232)
at android.app.Activity.performCreate(Activity.java:7221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4886)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4795)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:55)
at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:4845)
at android.app.ActivityThread.access00(ActivityThread.java:201)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1849)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
导航图和目的地使用相同的 ID 似乎是个问题。
https://issuetracker.google.com/issues/161825212
我们遇到了非常相似的崩溃,并通过为导航图使用唯一 ID 解决了这个问题。
我的项目中有两个活动 ActivityOne
和 MainActivity
。
我使用 ActivityOne
中的意图开始 MainActivity
,然后我在 MainActivity
中切换暗模式开关并在 activity 中调用 recreate()
。
但是 MainActivity
在重新创建时在 super.onCreate(savedInstanceState)
行崩溃。
androidx片段版本:1.2.5
生命周期版本:2.2.0
主要活动:
override fun onCreate(savedInstanceState: Bundle?) {
val userInterfaceMode = when (UIMode.currentUIMode) {
is UIMode.DarkMode -> AppCompatDelegate.MODE_NIGHT_YES
is UIMode.LightMode -> AppCompatDelegate.MODE_NIGHT_NO
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
AppCompatDelegate.setDefaultNightMode(userInterfaceMode)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
overridePendingTransition(
R.anim.enter_activity_fade_in_anim,
R.anim.enter_activity_fade_out_anim
)
if (savedInstanceState == null){
setupNavigation()
}
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
setupNavigation()
}
private fun setupNavigation(){
if (supportFragmentManager.findFragmentByTag(NAV_HOST_TAG) != null){
val host = supportFragmentManager
.findFragmentByTag(NAV_HOST_TAG) as NavHostFragment
supportFragmentManager
.beginTransaction()
.attach(host)
.setPrimaryNavigationFragment(host)
.commit()
}else{
val host = NavHostFragment.create(R.navigation.navigation_graph)
supportFragmentManager
.beginTransaction()
.add(
R.id.mainActivityRoot, host,
NAV_HOST_TAG
)
.setPrimaryNavigationFragment(host)
.commit()
}
}
Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.pitok.goodnews, PID: 6173
java.lang.RuntimeException: Unable to start activity ComponentInfo{MainActivity}: java.lang.IllegalArgumentException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4886)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4795)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:55)
at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:4845)
at android.app.ActivityThread.access00(ActivityThread.java:201)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1849)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.IllegalArgumentException
at androidx.lifecycle.LifecycleRegistry.downEvent(LifecycleRegistry.java:263)
at androidx.lifecycle.LifecycleRegistry.backwardPass(LifecycleRegistry.java:314)
at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:334)
at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
at androidx.lifecycle.LifecycleRegistry.setCurrentState(LifecycleRegistry.java:118)
at androidx.navigation.NavBackStackEntry.updateState(NavBackStackEntry.java:150)
at androidx.navigation.NavBackStackEntry.setMaxLifecycle(NavBackStackEntry.java:130)
at androidx.navigation.NavController.popBackStackInternal(NavController.java:325)
at androidx.navigation.NavController.dispatchOnDestinationChanged(NavController.java:426)
at androidx.navigation.NavController.onGraphCreated(NavController.java:636)
at androidx.navigation.NavController.setGraph(NavController.java:586)
at androidx.navigation.NavController.setGraph(NavController.java:551)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:247)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2685)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:280)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1368)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1446)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1509)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2637)
at androidx.fragment.app.FragmentManager.dispatchCreate(FragmentManager.java:2583)
at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:236)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:315)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:115)
at me.pitok.goodnews.appMain.MainActivity.onCreate(MainActivity.kt:24)
at android.app.Activity.performCreate(Activity.java:7232)
at android.app.Activity.performCreate(Activity.java:7221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4886)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4795)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ClientTransactionHandler.executeTransaction(ClientTransactionHandler.java:55)
at android.app.ActivityThread.handleRelaunchActivityLocally(ActivityThread.java:4845)
at android.app.ActivityThread.access00(ActivityThread.java:201)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1849)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
导航图和目的地使用相同的 ID 似乎是个问题。
https://issuetracker.google.com/issues/161825212
我们遇到了非常相似的崩溃,并通过为导航图使用唯一 ID 解决了这个问题。