Home > database >  [JETPACK COMPOSE]: is there a way to restart whole app programmatically?
[JETPACK COMPOSE]: is there a way to restart whole app programmatically?

Time:07-12

I need a way to make my app close and opens again. Or just restart everything inside of it. Since the issue with my app can be fixed by closing app and opening again (When creating user the loading function to firebase the collection inside user document does not load for some reason)

CodePudding user response:

Below code can do it:

 val context = LocalContext.current     
 val packageManager: PackageManager = context.packageManager
        val intent: Intent = packageManager.getLaunchIntentForPackage(context.packageName)!!
        val componentName: ComponentName = intent.component!!
        val restartIntent: Intent = Intent.makeRestartActivityTask(componentName)
        context.startActivity(restartIntent)
        Runtime.getRuntime().exit(0)

CodePudding user response:

So... you could write ways to "restart everything", but I am going to strongly suggest not doing that. When building an app, you will run into edge cases. You will hit some failure states that you can't recover from. That happens. But, solving these cases by just "restarting" doesn't actually address the issue and will probably result in wonky and undesirable UX.

I would instead suggest you post here some steps to reproduce and debugging results.

  • Under what circumstance/flow does this document stop loading?
  • When it does happen, can you capture the call to Firebase where it is trying to load the document?
    • If so, what does that call look like?
    • Is it looking for the document in the correct place?
    • Can you show an example of the call when it fails and also what the database hierarchy looks like?
  • Can you show us a snippet of code where this is failing?

Without more information, this sounds more like a race condition - you might be trying to find the document before it is actually created and not handling the error path for when the document is not found. However, we would need to do some debugging to verify.

  • Related