1

I'm using my appDelegate's applicationDidReceiveMemoryWarning method to dump some high resource objects. When the app starts back up, rather than trying to reload just those objects and return the user to his last page, I would like just to restart the app from the top (from the main page, the app only goes one level deep, so this is totally acceptable to us).

Here's my paltry attempt, but it was an abject failure. It got close, but I ended up introducing some problems that resulted in an actual memory dump and app crash.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.viewController = [[SplashScreenViewController alloc] initWithNibName:@"SplashScreenViewController" bundle:nil];
    UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = navcon;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    //For testing purposes only
    self.lowMemoryWarning = TRUE;
    NSLog(@"app did enter background");
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSLog(@"app will enter foreground");
    if (self.lowMemoryWarning) {
        NSLog(@"recovering from low memory warning");
        self.window.rootViewController = nil;
        UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:self.viewController];
        self.window.rootViewController = navcon;
        [self.window makeKeyAndVisible];
    }
}

What's the best approach for doing something like this? Is there maybe a simple trick that I don't know about?

Thank you!

Justin Whitney
  • 1,242
  • 11
  • 17
  • I don't think it's even possible in iOS... – iProgrammed Jan 05 '13 at 16:29
  • As an alternative, it would be lovely to pop a `UIActivityIndicator` onto the current view while I load certain objects back into memory. But I've tried both `UIViewController *currentVC = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject]` and `UIViewController *currentVC = [[UIApplication sharedApplication] keyWindow].rootViewController;` trying to get at that VC and neither seems to work. – Justin Whitney Jan 06 '13 at 15:35

1 Answers1

0

Do you mean you want the app to restart each time it started (or enter foreground) ? If yes, maybe you can just set the app to not support multi tasking

http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW5

Search for "Opting out of Background Execution"

================================================================================

Ah sorry, I didn't know the nature of your application, if some process need to be run on the background then this method is no go.

I read your comment above about the UIActivityIndicator + loading the certain objects back on the current view after app enter foreground. Maybe this thread can help you, Finding the current view when application enter foreground. IOS

Community
  • 1
  • 1
ewiinnnnn
  • 995
  • 7
  • 7
  • That would be a good solution except (small detail I didn't mention -sorry), the primary function of this app IS to run in the background. It's an audio app but has some heavy visual elements that can be released in a low memory situation. So when I get low memory, I want to release some images but retain the audio, which could be playing at that moment, rather than letting iOS shut the whole thing down. Then, when the app starts back up, I want to go back to the splash page so those visual elements can be reloaded. Make sense? – Justin Whitney Jan 06 '13 at 15:33