1

I've been trying without any luck to implement state restoration and preservation.

I'm not using storyboards. I have to be doing something wrong.

I set up the restoration identifier for every Nib file.

I know the state is being stored, but never restored properly. The last state shows for half a second and then it goes back to the main view. I've tried many things with no luck. Please help!

-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"will finish");
    return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

    navigator = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = navigator;

    [self.window makeKeyAndVisible];
    [navigator release];

    NSLog(@"did finish");
    return YES;
}
// As you can see, I started the process of saving and restoring application state.
// Also, I added the restoration identifier for every class that should be restored.

-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
    NSLog(@"restoring");
    return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
    return YES;
}
-(void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder
{
    NSLog(@"restored");
    //navigator = [[UINavigationController alloc] initWithRootViewController:];

    isRestored = YES;
}
Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Paradox
  • 94
  • 6
  • I have not yet had a reason to work on integrating state saving/restoration, but it occurs to me that you are likely stomping all over your resumed state in your application:didFinishLaunchingWithOptions: method. What order are your NSLog()s showing up in console? – Christian L Oct 08 '12 at 01:58
  • Christian, thank you for your answer. The NSLogs show up in this order: "will finish" "restoring" "restored" "did finish" I'm sure that what you pointed out is true. The thing is I'm not sure how to solve it. I've tried many things. My experience in iOS is limited. – Paradox Oct 08 '12 at 15:43
  • 1
    I have tried to make it work without Storyboards with no success. It looks like Storyboards are taking care of a lot of work, and doing in in code requires you to write the actual saving code. I might be wrong though. http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/StatePreservation/StatePreservation.html – sachadso Dec 09 '12 at 14:43

1 Answers1

0

You should do your controllers initialization in application:willFinishLaunchingWithOptions: because restoration is done before application:didFinishLaunchingWithOptions: is called.

Check this answer.

Also you should programmatically assign restorationIdentifier to all controllers.

Community
  • 1
  • 1
Numeral
  • 1,405
  • 12
  • 24