【问题标题】:Save current state while fast context switching - iOS4在快速上下文切换时保存当前状态 - iOS4
【发布时间】:2010-07-02 11:59:34
【问题描述】:

在通过 iOS-4 多任务处理进行快速上下文切换时,我对保存应用程序的最后状态存有疑问。

应用程序是否必须手动将最后一个状态保存在“- (void)applicationDidEnterBackground:(UIApplication *)application”中?还是 iOS-4 会处理它?

在视频中提到如下:

-(void)applicationDidEnterBackground:(UIApplication *)application {
   // save app state
   [self saveState];

   // reduce memory usages
   ....

   // prepare UI
   ....

   // close listening sockets
   ....
}

提前致谢,

苏尼尔

【问题讨论】:

    标签: iphone ios4


    【解决方案1】:

    一旦您的应用程序进入后台,就无法保证它会再次回到前台。它可以在任何时间点终止,恕不另行通知。因此,进入后台时,您希望保存状态,否则可能会丢失它。

    引用苹果(来源:http://developer.apple.com/iphone/library/documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html),

    在移动到后台之前保存您的应用程序状态。在内存不足的情况下,后台应用程序会从内存中清除以释放空间。暂停的应用程序首先被清除,并且在清除之前不会通知应用程序。因此,在移至后台之前,应用程序应始终保存足够的状态信息,以便以后在必要时进行自我重组。将您的应用程序恢复到之前的状态也为用户提供了一致性,当应用程序重新启动时,他们将短暂地看到应用程序主窗口的快照。

    【讨论】:

      【解决方案2】:

      应用程序是否必须手动将最后一个状态保存在“- (void)applicationDidEnterBackground:(UIApplication *)application”中?还是 iOS-4 会处理它?

      是的,如果您希望您的应用在被杀死后恢复,您需要在此处手动保存状态。

      【讨论】:

        【解决方案3】:

        在进入后台/终止之前,您应该在应用程序委托中使用这两种方法来保存当前状态。

          - (void)applicationWillResignActive:(UIApplication *)application {
        
            /*
             Sent when the application is about to move from active to inactive state. 
             This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) 
             or when the user quits the application and it begins the transition to the background state.
             Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. 
             Games should use this method to pause the game.
             */
        }   
           - (void)applicationWillTerminate:(UIApplication *)application {
        
            /*
             Called when the application is about to terminate.
             See also applicationDidEnterBackground:.
             */
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-05
          • 1970-01-01
          • 2021-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-25
          • 1970-01-01
          相关资源
          最近更新 更多