I'm trying to understand how the Flutter app lifecycle works and one question that's still not clear to me is how Flutter (and FCM) handles background messages. The docs state that the background messages listener "must be a top-level function and cannot be anonymous" and that it runs in its own isolate, but I can't find any more info about this isolate.
I experimented with it and it seems that this isolate still contains all the variables from the global scope, but no state is kept from the app when it was in the foreground and it cannot be updated either.
My specific questions are:
- Is this isolate initialized by running all the code in the global scope every time a message is received in the background (e.g., if I have a global variable
final instance = thirdPartyLibrary.getInstance(), will that try to initialize that thrid-party library each time a message is received) or is the state of this isolate saved and restored with each message? - Is it a good practice to limit the amount of code run by initializing the global variables? (This can be especially important with some Android ROMs, which severely limit the time spent in the background for an app and start sending notifications to the user if it consumes too much energy in the background)
Is there any good resource that describes how the initialization process looks like (both in case of moving to/from the background and closing/force-closing/opening the app)? Which part of the application is run in which case and what state is kept from the running app (e.g., force-closing an app closes it "more" - the notifications don't work anymore - but what does that mean)?
Thank you for your answers.