How to force Flutter to set app lifecycle state - dart

As didChangeAppLifecycleState is able to know when app lifecycle state has changed [#50131598]; I want to force my flutter app to be in an specific lifecycle state.
Is there a method such as setLifeCycleState(AppLifecycleState.paused); ?
I want to force my running app to paused itself.
Any suggestion will be welcome !

Related

is it possible to keep flutter app do not killed by iOS when running in background

I was wonder if possible to keep the app always rescue from backgroud? I am use an app writed using flutter in iOS, sometimes when I switched this app to the background, when next use it, will open a fresh state and lost the last state. is it possible to make the app always could keep the last state except exit app by user. So that the app would keep all the state of the last seem by the user.
You cannot prevent the operating system from killing your app. iOS kills apps to increase battery life and to free memory.
What you can do is to store some state to the file system with packages like shared_preferences or hive.
If you are using BLoC, you might also find the hydrated_bloc package useful.

Is there any delegate which fires while app is killed from background only?

Is there any delegate which fires while app is killed from background in iPhone X?
No. Your app is suspended. It can't get any events because it isn't running. If the app is running in the background, you might get applicationWillTerminate: if the user terminates the app, but I wouldn't count on it. Basically you should assume when the app goes into the background that it might be terminated.
Yes there is please refer to Pushkit framework, I have done that same task using this library, Here]1 is the link all you need to set up, if you want to try in kill mode, you need to do this on Xcode => go to edit scheme in your Xcode.
After setting all this you will be able to do all you need

When iOS terminates an app which ViewController is started when the app opens up again?

In android when an application is terminated by the os, the activity stack remains intact when the app is opened up again. Does something similar happen when an ios app is terminated and reopened, or does the app behave as if it's being opened from a cold start?
You can use the concept of state restoration. There is restoration ID for every view controller in storyboard. When the App is terminated and launched again then you can navigate to the same state where the App has terminated by using the concept of restoration. You can go through the below link for to get the steps of implementation
https://www.raywenderlich.com/117471/state-restoration-tutorial
https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/PreservingandRestoringState.html
Oops- better answers below. We have both learned something new today
cold start. If you want to restore state, you will need to keep a track of where you are, and what data has been loaded (maybe in NSUserDefault) and restore state manually

Is there a way to know when my app switches from Suspended to another state?

I'm having issues with my WKWebViews'cache being purged sometimes when a user hasn't been using my app for a while.
I guess, that's because iOS puts my app in Suspended and then a low-memory condition occurs so my app is purged.
I read Apple's documentation about The App Life Cycle - Execution States for Apps
Especially the part describing the Suspended state :
Suspended - The app is in the background but is not executing code. The system
moves apps to this state automatically and does not notify them before
doing so. While suspended, an app remains in memory but does not
execute any code. When a low-memory condition occurs, the system may
purge suspended apps without notice to make more space for the
foreground app.
Two questions here :
1) What does "purge" mean exactly ? The app is killed ? Or does it still appear on the app selector ( when double-tapping the home button )
2) Here we can see that there is no way to know when an app is going to be suspended. All right. But if a user comes back to the app, is there a way to know, then, that the app has been suspended ? I can't find any UIAppDelegate's method to do so, but there might be another solution ?
Purge - The purge command forces disk and memory caches to be emptied,
offering a ‘cold disk buffer cache’ which is similar to the state of
the operating system after a reboot.
Referenced from here
As per the documentation
applicationDidEnterBackground:—Lets you know that your app is now
running in the background and may be suspended at any time.
This is the only method which will let you know that method may will enter in Suspended State
also
application:didFinishLaunchingWithOptions: will let you now that
your app's launch process is almost done and the app is almost ready
to run.
The app is in the background but is not executing code
I think it's explained good by Apple, the app is still in memory but no background threads are executed since foreground app requires more resources. Maybe when memory is restored, your app can execute background code again if has not been killed by OS.
When low-memory condition occurs, you're app could be killed (purge).
Is there a way to know, then, that the app has been suspended?
When app is killed from OS, next launch is equal to a complete new restart of app. For this AppDelegate doesn't provide a method to achieve this. You could set a flag in UserDefaults when app goes in background and then make some logic on that flag.

iOS 6 Saving/Restoring app state feature

I'm trying to use the new Saving/Restoring app state feature on iOS 6, but (application:shouldRestoreApplicationState:) method is not called if I killed the app so it doesn't restore the app state, but If I'm running it from the debugger it gets called.
It worked when I added (Application does not run in background) in the plist and set it to YES, I don't want to set it to YES though. I was wondering if anyone got it working without setting the (Application does not run in background) to YES.
From the Apple docs:
The system automatically deletes an app’s preserved state when the user force quits the app. Deleting the preserved state information when the app is killed is a safety precaution. (As a safety precaution, the system also deletes preserved state if the app crashes twice during launch.) If you want to test your app’s ability to restore its state, you should not use the multitasking bar to kill the app during debugging. Instead, use Xcode to kill the app or kill the app programmatically by installing a temporary command or gesture to call exit on demand.
From experience, the easiest way is to put your app into the background by pressing the home button (or Command+Shift+H for the simulator). Then use the Xcode stop button. As the docs suggests, a debug exit gesture also works.
Update:
I found a small work around. The app saves its state in Library/Saved Application State/[Bundle ID]-[App Name].savedState/data.data.
When using the simulator, you can copy this file and drop it in anytime you want to restore to that specific state.
Similarly, on a device you can generate a .xcappdata archive from the organizer. Then, you can upload the .xcappdata archive when you want to restore from that saved state.

Resources