why my iOS app restarts from first screen after coming back to foreground mode?

3k Views Asked by At

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?

1

There are 1 best solutions below

1
On

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