What method is called after "applicationDidBecomeActive"?

This is my first question here, as I had a problem developing my first iOS application. This is one of thousands of flashlight apps, however I try to use as many features as possible. One of them saves the state of the application when it goes to the background or ends. After going to the foreground (iOS 4 or higher) or restarting, I load the settings from the file and reapplying them. One of the settings is obviously AVCaptureDevice.torchMode . However, I ran into this problem. I reapply these parameters in the applicationDidBecomeActive method. Everything seems to work, but when I press the home button very quickly and then run the application again, the application will do the following (I delayed the applicationDidBecomeActive method to watch it):

1. shows a black screen (loading)
2. executes applicationDidBecomeActive and turns on the LED (I put my delay here)
3. shows the current current of the UIViewController and at the same time turns off the LED

This happens only after the application is called from the background immediately after it is sent. I know this is not a realistic use case, but I like to think that errors often “add up”, and due to this (possibly) poor design, I may encounter other problems in the future. I am absolutely sure that this is not my code that turns off the LED with i NSLog , when my code changes the property AVCaptureDevice.torchMode . So, more precisely, my question is:
Which method is called after applicationDidBecomeActive , possibly related to the UIViewController , which can turn off my flashlight? And is there any possible solution or workaround there?

+3
source share
2 answers

According to iOS Application Programming Guide :

Returning to the forefront, your applications can restart the tasks that it stopped when it moved to the background. The steps that occur when moving to the foreground are shown in Figure 3-6. The applicationWillEnterForeground: method should undo everything that was done in your application. The DidEnterBackground: method and the applicationDidBecomeActive: method should continue to execute the same that will be executed at startup .

Have you tried to reapply your settings in the applicationDidBecomeActive: method instead of applicationWillEnterForeground: :?

Another thing to consider is working with notifications:

In the applicationDidBecomeActive: or applicationDidBecomeActive: AppDelegate methods, you can tell your application delegate to send notifications to your controllers:

 - (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. */ // Dispatch notification to controllers [[NSNotificationCenter defaultCenter] postNotificationName: @"didBecomeActive" object: nil userInfo: nil]; } 

After that, the view controller can register for these notifications (for example, in their init method):

 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(loadSettings) name: @"didBecomeActive" object: nil]; 

This way, your controller knows that the application has just become active and can execute any method you want.

In this example, you are telling your view controller to execute the loadSettings method when it receives a didBecomeActive notification (which was sent by the application delegate).

+5
source

I only have an answer why a quick launch of the application will display a black screen, I have no link only to personal experience and observation.

When the application is sent to the background, the OS tries to take a screenshot to use instead of Default.png . If you run the application before the picture is taken and you do not have Default.png in your project, you will get this.

Still thinking about your actual question.

+3
source

Source: https://habr.com/ru/post/1389911/


All Articles