Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 574
Hiren Prajapati Avatar asked Oct 11 '17 08:10

Hiren Prajapati


People also ask

Why does my app restart whenever I come back to it from another app iOS?

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.

How do I stop iOS apps from restarting?

Stop apps refreshing in the backgroundTap on Settings on your iPhone or iPad > General > Background App Refresh > toggle Background App Refresh to Off.

Why do my apps keep closing in the background iPhone?

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.


1 Answers

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

like image 156
Mike Atkins-Spelling Avatar answered Oct 04 '22 16:10

Mike Atkins-Spelling