Search code examples
iosmemory-managementuinavigationcontrollerbackground-processrootviewcontroller

iOS app returns to rootViewController when kept in background


My iOS app returns back to rootViewController when kept in background for longer time. To demonstrate I have the below picture enter image description here

I navigate all the way to ViewControllerC and keeps the app in background, when I returns to the app say after 30 mins, the app shows rootViewController i.e. ViewControllerA.

I want it to remain on ViewControllerC as it stays in Whatsapp. I know that my app (idle apps) will be removed from the memory when system falls short in memory, so when I open the app it will take me back to the initial View controller specified. But then why this doesn't happen with whatsapp?


Solution

  • Apple provides a solution to this situation: UIViewController, along with the App Delegate, has methods permitting you to save and restore state. When the app goes into the background, the current configuration (what view controller's view is showing) is saved. That way, even when the app quits, when it relaunches it can get back to that configuration before it appears to the user. Thus, coming back from background-and-quit looks just like coming back from mere backgrounding.

    For full details, see Apple's documentation. This is a good place to start:

    https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/PreservingandRestoringState.html