When in My iOS app which which is in the detail screen and I press home button which will result it in going to background mode. After inactivity of 7 around minutes, I relaunch it and it doesn't start from where I left it. It starts from the first screen.
I hit the internet and came to know about state preservation and restoration. I implemented in one screen but it doesn't seem to work. This is what I did in appDelegate.m
//appDelegate.m
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
Following code is in appDelegate.m in willFinishLaunchingWithOptions method. I am not using storyboard as this app is very old. It has XIBs. So this app always needs to go to login screen where it is checked if accessToken is stored, it will go to home screen from login screen. If not stored, it will remain in login screen. So this is mandatory to perform. Thus there is only one way to code this like below.
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
...
loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:loginViewController];
self.navigationController.restorationIdentifier = @"NavigationController";
[loginViewController.view setBackgroundColor:[UIColor whiteColor]];
self.window.rootViewController = self.navigationController;
...
...
}
I have given restorationId to all view controller like below in viewDidLoad(). For example This is what I did in PetDetailViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.restorationIdentifier = @"MatchedPetIdentification";
self.restorationClass = [self class];
}
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[super encodeRestorableStateWithCoder:coder];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
[super decodeRestorableStateWithCoder:coder];
}
Now when I go to PetDetail screen and press home button, encodeRestorableStateWithCoder() gets called. Stopping app from xcode, relaunching it stays on same screen but immediately goes to login screen and transit to home screen(code in willFinishLaunchingWithOptions might be executing)
Am I doing anything wrong? How to prevent app from restarting from first screen unless user kills it manually?
When your phone needs the RAM for another task (the app that you're running in the foreground also needs RAM), it will kill off other apps. That's why when you relaunch those other apps, they need to be reload again.
Stop apps refreshing in the backgroundTap on Settings on your iPhone or iPad > General > Background App Refresh > toggle Background App Refresh to Off.
Random and frequent app crashes in mobile devices usually denote a memory issue like when the device is running low on storage. Other performance issues including symptoms of sluggishness, unresponsiveness and random restarts are also likely to transpire in this case.
You cannot control when your app is put into a suspended state from the background state, the OS will automatically do this to allow more memory for foreground apps. For more information on state transitions and app termination you can refer to:
https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With