2016-11-24 76 views
2

我的應用程序在後臺運行,如果我再次打開,它會顯示我離開它的相同頁面。進入Suspended狀態後,會調用AppDelegate的哪些方法?

雖然,如果iOS將應用程序置於暫停狀態,但它仍在內存中。如果我回來,哪些AppDelegate方法將被調用。

其實我的目的是從暫停的應用程序恢復同樣的屏幕,如果它不是終止。

最後一件事,如果App從SUSPENDED狀態返回,將會調用didFinishLaunchWithOptions。

謝謝..

+1

在我看來,在這種情況下最好使用iOS的[保存和恢復狀態](https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/PreservingandRestoringState.html)功能。 – dive

+0

如果應用程序被殺害而不是開始,它會去恢復ViewController,或者它會從第一個開始? @潛水 –

回答

3

由於Apple Documentation狀態,

  • 應用:willFinishLaunchingWithOptions: - 此方法是你的應用首次在啓動時執行代碼的機會。

  • application:didFinishLaunchingWithOptions: - 此方法允許您在應用程序顯示爲 之前執行任何最終初始化。

  • applicationDidBecomeActive:-Lets你的應用程序知道它即將成爲前臺應用程序。使用此方法進行任何最後時刻的準備工作

  • applicationWillResignActive:-Lets你知道你的應用程序來自於正在前臺應用程序轉變了。到
    把你的應用程序進入靜止狀態使用此方法。

  • applicationDidEnterBackground:-Lets您知道您的應用程序現在在後臺運行,並可以在任何時間暫停。

  • applicationWillEnterForeground: - 讓您知道您的應用正在移出背景並返回前景,但該 尚未激活。

  • applicationWillTerminate:-Lets你知道你的應用程序被終止。如果您的應用已被暫停,則不會調用此方法。

所以applicationWillEnterForegroundapplicationWillResignActive將被調用!

1
- (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 invalidate graphics rendering callbacks. Games should use this method to pause the game. 
} 


- (void)applicationDidEnterBackground:(UIApplication *)application { 
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
} 


- (void)applicationWillEnterForeground:(UIApplication *)application { 
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
} 


- (void)applicationDidBecomeActive:(UIApplication *)application { 
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
} 


- (void)applicationWillTerminate:(UIApplication *)application { 
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 

didFinishLaunchWithOptions didnt電話。

0

根據application's life cycle,當ios將您的應用程序置於暫停模式時,您的應用程序將不會收到任何通知。每當你的應用程序進入後臺模式,並且如果它沒有做任何事情 - 不處理 - ios會將其置於暫停狀態。 但是當被暫停並且它仍然在內存中時,你並不需要做任何事情來顯示你的應用程序之前的相同屏幕。 ios自動保持應用程序的狀態。只有當您的應用在暫停模式下終止時,您才需要進行管理..i.e。不在記憶中。

如果你沒有在後臺與任何background execution方法的任何執行,你可以,如果你收到通知您的應用程序商店applicationDidEnterBackground狀態的地方,並applicationWillEnterForeground可以顯示與存儲狀態的應用程序考慮掛起模式的應用程序。

或者如果您在後臺執行一些有限的任務,您可以保留本地變量並使用它來跟蹤暫停或現在。在applicationDidEnterBackground,variable = inBackground,當你完成任務並且variable == inBackground,設置variable == inSuspended並且在某處存儲你的應用的狀態。 on applicationWillEnterForeground

if variable == inSuspended 
    { 
    //Display app according to previously stored state. 
    `variable == inForgorund`, 
} 
0

您可以使用AppDelegate文件中的斷點測試您的自我。

如果用戶點擊主頁按鈕一次,應用程序處於暫停狀態。 如果用戶將兩次單擊主頁按鈕,應用程序處於非活動狀態。

當用戶從暫停狀態進入應用程序時,我發現下面的方法調用。

first:applicationWillEnterForeground then applicationDidBecomeActive

didFinishLaunchingWithOptions不叫。

More details

相關問題