I have two activities in my project ActivityOne
and MainActivity
.
I start MainActivity
using intent from ActivityOne
then I toggle dark mode switch in the MainActivity
and call recreate()
on the activity.
but MainActivity
crashes on super.onCreate(savedInstanceState)
line when it recreated.
androidx fragment version: 1.2.5
lifecycle version: 2.2.0
MainActivity:
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.access$3300(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.access$3300(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)
Seems to be an issue using the same id for the navigation graph and the destination.
https://issuetracker.google.com/issues/161825212
We had a very similar crash and solved it by using a unique id for the navigation graph.