简体   繁体   中英

App restarts when resuming from background after some time

The scenario is when the app is on Fragment A and then kept the app in the background for some time. After resuming from the background the initial state of the app is in Fragment A but then suddenly app restarts from startDestination in the navigation component causing the below crash.

 Fatal Exception: java.lang.IllegalArgumentException: Navigation action/destination com.app.gulfcraftv2:id/action_splashFragment_to_nav_home cannot be found from the current destination Destination(com.app.gulfcraftv2:id/companyFragment) class=com.precise.gulfcraft.ui.companydetail.CompanyDetailFragment
   at androidx.navigation.NavController.navigate(NavController.kt:1540)
   at androidx.navigation.NavController.navigate(NavController.kt:1472)
   at androidx.navigation.NavController.navigate(NavController.kt:1930)
   at com.precise.gulfcraft.ui.splash.SplashFragment.onCreate$lambda-6(SplashFragment.kt:75)
   at com.precise.gulfcraft.ui.splash.SplashFragment.lambda$Aso8YI02DbFhfWptH-qrBUU0ulU()
   at com.precise.gulfcraft.ui.splash.-$$Lambda$SplashFragment$Aso8YI02DbFhfWptH-qrBUU0ulU.run(:2)
   at android.os.Handler.handleCallback(Handler.java:883)
   at android.os.Handler.dispatchMessage(Handler.java:100)
   at android.os.Looper.loop(Looper.java:224)
   at android.app.ActivityThread.main(ActivityThread.java:7562)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Note - The launch mode used is singleTop

The crash is happening in the below section.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    try {
        Handler(Looper.getMainLooper()).postDelayed({
            findNavController().navigate(SplashFragmentDirections.actionSplashFragmentToNavHome())
        }, 3000)
    } catch (e: Exception) {
    }
}

check first if this is your current destination with following function

fun navigate(destination: NavDirections) = with(findNavController()) {
    currentDestination?.getAction(destination.actionId)
        ?.let { navigate(destination) }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM