低内存警告后重启应用程序

时间:2013-01-05 14:19:40

标签: iphone ios

我正在使用appDelegate的applicationDidReceiveMemoryWarning方法转储一些高资源对象。当应用程序开始备份时,我不想尝试重新加载这些对象并将用户返回到他的最后一页,我只想从顶部重启应用程序(从主页面开始,应用程序只有一层深,所以这完全可以接受我们。)

这是我的微不足道的尝试,但这是一个严重的失败。它接近了,但最后我引入了一些导致实际内存转储和应用程序崩溃的问题。

- (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];
    }
}

做这样的事情的最佳方法是什么?是否有一个我不知道的简单技巧?

谢谢!

1 个答案:

答案 0 :(得分:0)

你的意思是你希望应用程序每次启动时重启(或进入前台)?如果是,也许您可​​以将应用设置为不支持多任务

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

搜索“退出后台执行”

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

对不起,我不知道您的应用程序的性质,如果某个进程需要在后台运行,那么这个方法就不行了。

我在上面阅读了关于UIActivityIndi​​cator的评论+在应用程序进入前景后将某些对象加载回当前视图。也许这个主题可以帮助你,Finding the current view when application enter foreground. IOS