Does OnNavigatedTo get called when you turn on the screen and the app comes to life? - event-handling

I'm dealing with a strange situation I want to debug in my Windows Phone 8.1 app, and I'm not sure in which moments OnNavigatedTo is called.
Obviously, it gets called (and I've checked tracing it with the debugger) when you navigate to the view normally.
My doubt arises in other point I want to check, let's call it "You wake up your application and the screen was shut off".
My question is: When you turn on the screen, and you swype the screen protector away, is "OnNavigatedTo" function called or not?
According to some manuals I've read somewhere else, it should.
According to my Debug.Writeline traces, it seems it doesn't.
I need to check some condition and execute some code before/when the view appears, and I'm unable to do it properly.
PS: Does it exist some other alternative event I should use to deal with "This view is becoming visible/focused after you turn the screen on" situation instead of "OnNavigatedTo" ?
Thanks in advance.

In Windows Phone 8.1 Runtime (Store apps) OnNavigatedTo is called only during navigation. It's not called after resuming from suspension - you can read a reference here at MSDN:
before Suspending event, the OnNavigatedFrom event is being called, but when you Resume, the OnNavigatedTo is not called
In your case when you lock the screen, the app is suspended, after you resume OnNavigatedTo is not called. If you look for some events which may be called - take a look at Window.Activated and Window.VisibilityChanged events.
The other case is that when you debug your app, your app won't be suspended, you will need to test it via Lifecycle events tab.

Related

Why is asking for camera/microphone permission triggering applicationDidBecomeActive?

I've taken over a project that handles which screen to show first in the "applicationDidBecomeActive" function inside the AppDelegate. This has been causing problems because whenever the app asks for a permission sometimes, not all, it will trigger this block of code and send the user to the incorrect screen. I'm not sure what to do since I've never encountered this before. Any ideas why this is occurring or ways to circumvent this? I've tried adding a flag variable, however, that doesn't seem to work consistently because it looks like it gets un-set before the applicationDidBecomeActive is triggered.
When system notifications such as Permissions are presented, the application is sent to background, or becomes "inactive". When the Permission notification is dismissed, the application becomes "active" again. This would explain why didBecomeActive is triggered.
https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622956-applicationdidbecomeactive
I've taken over a project that handles which screen to show first in the "applicationDidBecomeActive" function inside the AppDelegate... Any ideas why this is occurring or ways to circumvent this?
The simple answer is: don't do that work in applicationDidBecomeActive(). As you've seen, the application can become inactive and then active again while the app remains in the foreground, so that's not the right state transition for what you're doing. Identify the app state transitions where you really do want to choose a screen. For example, you might want to do it when the app launches, and also when it transitions from background to foreground, so you could use application(_:didFinishLaunchingWithOptions:) and applicationWillEnterForeground(_:). (Obviously, you don't want to put the same code in both places, so put it in a separate method and just call it from those two methods.)
Apple has a document that covers this very topic: Managing Your App's Life Cycle. There's also a lot of information on application states on the UIApplicationDelegate reference page. Both documents include helpful state transition diagrams and descriptions of what the various states mean.
As a solution; you can use the applicationWillEnterForeground(). It's being called before the applicationDidBecomeActive() and not being triggered by permission request.

In Corona SDK How to hide a group if application suspended?

I am building a word game and I want to hide the board when application is suspended?
the code looks fine however it givs a strange behaviour!!,
when I suspend the app nothing will happen but when i resume the application then the board will hide!!
local onSystem = function( event )
if event.type == "applicationSuspend" then
print("suspend")
board_group.alpha = 0
end
end
Runtime:addEventListener( "system", onSystem )
Note: you might wonder how do I know how the application looks when suspended?
the answer is: by pressing the home button twice.
example
SpellTower in normal state
https://dzwonsemrish7.cloudfront.net/items/430k0c0b0y0b413d0b42/Image%202012.11.12%208:08:24%20AM.png?v=4822f549
SpellTower after pressing the home button twice
https://dzwonsemrish7.cloudfront.net/items/280a1y0r2U3W321y1B2z/Image%202012.11.12%208:08:31%20AM.png?v=09c37567
you can see how they are hiding the letters, this is exactly what I want to do for my game, the only difference is i am using Corona SDK
When you do board_group.alpha = 0 you only has set a variable, the result will only take effect after a screen update.
But since the application is suspended... it won't update! So, changing any graphics on applicationSuspend don't work.
I believe the reason is because the application is not considered as suspended. In normal objective c programming it means that applicationWillResignActive is called when the user double clicks on the home button. So what you want to do is to add that code for this part.
Here is a flow of events:
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
Corona seems to have these events:
"applicationStart" occurs when the application is launched and all code
in main.lua is executed.
"applicationExit" occurs when the user quits the application.
"applicationSuspend" occurs when the device needs to suspend the application such as during a phone call or if the phone goes to sleep
from inactivity. In the simulator, this corresponds to the simulator
running in the background. During suspension, no events (not even
enterFrame events) are sent to the application while suspended, so if
you have code that depends on time, you should account for the time
lost to an application being suspended.
"applicationResume" occurs when the application resumes after a suspend. On the phone, this occurs if the application was suspended
because of a phone call. On the simulator, this occurs when the simulator was in the background and now is the foreground application.
So my guess is that you have to implement it outside of the corona API.
According to the corona documents you can implement them in the delegate:
You can intercept UIApplicationDelegate events via your implementation
of the CoronaDelegate protocol.
This protocol conforms to the UIApplicationDelegate protocol. Corona's
internal delegate will call your protocol's method if it is
implemented.
Please keep in mind the following:
Methods that Apple has deprecated will be ignored.
In most cases, your class' version will be invoked after Corona's corresponding version of the UIApplicationDelegate method. There is one situation in which your version will be called before.
In situations where the app is about to suspend or go to the background, your method will be called before Corona's version, e.g.
applicationWillResignActive: and applicationDidEnterBackground:.
http://docs.coronalabs.com/native/enterprise/ios/CoronaDelegate.html
But this is just a guess. Hope it helps!
Edit:
I was thinking, something really simple you could do is catch it outside and present a "pause" screen, then just hide it when the application enters foreground.
So if you can't do that (for now), one other option is to save application state when the application is about to terminate, and then set UIApplicationExitsOnSuspend = true in your plist file. This will cause the application to exit instead of suspending, which will avoid any screenshots, effectively "hiding" the board, etc. The downfall is, the app will have to read the session state when it launches again... this is only useful if your application can be designed to actually exit without losing your state, and is quite honestly, a little extreme. That said, it may be the only way to effectively do what you're trying to do.
Other ideas would be to see if you can add a large black layer to the screen, even though the application is suspending; perhaps this will somehow trigger an internal screen update by natively setting setNeedsDisplay. Also, instead of modifying the alpha, you might consider temporarily removing all of your layers and see if that has a similar effect.

Programming background behavior when iPhone is locked and user presses power button

Sorry, I am new to iPhone development and my google searches have failed me, so I have had to resort to posting a potentially idiotic question on SO.
I would like to write an app that, when suspended, performs an action when the user presses the home or power button (presumably to unlock the phone). I read the section in the iOS App Programming Guide's App States and Multitasking and the "Processing Queued Notifications at Wakeup Time" discusses handling queued events upon waking up. However, this isn't what I'm after.
I'd like to know if it is possible to:
From the phone sleeping state (I couldn't find a document for phone states, so I am talking about the case where the user presses the power button to turn off the screen), I would like my app to be ready to respond to the event where the user presses the power or home button (to unlock the phone)
I would like my app to respond to the event that occurs when the user unlocks the phone. I found an answer here that's close.
I don't want any funkiness when calls come in, get ignored, etc. :)
From what I can tell, it's a very gray area in the API around locking and unlocking, and I'd like to verify whether or not I'm wasting my time trying to do this.
It looks like I can use the accelerometer to detect when the phone is locked, but I also assume that I won't be able to count on this behavior in all future versions of iOS.
EDIT - I think I can handle the locking and unlocking requirements by assuming that the application has to be running at the time the phone is locked and unlocked, but I still cannot figure out if it is possible to determine #1 above, which is that the power button has been pressed and the unlock screen is displayed. Likewise, I'd like to know if the power button has been pressed again and is no longer displayed (i.e. screen is off).
All of the behaviors for your app being suspended/backgrounded are in the document you referenced. The AppDelegate will receive these messages and pass them on to whatever view you want to listen for them.
-applicationWillTerminate
-applicationWillEnterBackground
-applicationDidBecomeActive
-applicationWillResignActive
You can set up a notification observer in your view if you would like the view to be notified of any of these events. Then just override them or set up a custom method to do any work that needs to be done.
While the Apple docs might be a little foggy at first, all the information is in there to let you know which state your app will go to and how it will be handled. It is up to you to figure out what your app needs to do for each of these events. Hope this helps.

How can I capture the "Stop" button being pressed in the iOS debugger?

I'm debugging an iOS app, and I need to call some cleanup code in some C++ classes (really, i just want their destructors to be called). When I run the app via XCode with the device attached, and then stop it by pressing the stop button, none of the app delegate methods are called (I'm looking specifically at applicationWillTerminate). However, if I pick up the device and press the home button, it successfully calls both applicationDidEnterBackground and applicationWillResignActive.
What am I doing wrong? I've tried removing all references to any C++ code, effectively making a blank iOS app, and I simply cannot get applicationWillTerminate to be called, or even dealloc in the root view controller. I've even tried using the signal() method along with SIGABRT, SIGKILL, etc., and that does nothing (plus, I can't call the Objective-C methods from a C method anyway). How can I react to the stop button being pressed?
You aren't doing anything wrong, the system just kills your app when you press on the stop button.
There are no methods called because the app is just killed, the same thing can happen when you app is backgrouded and the system needs more memory.
For more precision for the methods called when the app exits / enter in background, check this page
Like #rckoenes said, when you stop the app with the debugger, it kills it.
If your app has the background capability (UIApplicationExitsOnSuspend = NO, it's the default mode), when you quit your app, it is placed in the background instead terminated.

Iphone Calendar Application

i try to retrieve Calendar events between two dates. After that I try to delete all events. this program is working fine. The Events deleting process will take few second/minute based on number of events.
When device goes to sleep this application will work in background as usual. After coming to normal state(wake up) my application will exit. But, i did over come this problem, disable idle time.
But this same problem repeating when i press sleep/wake up button on the top of iPhone. How to over come this problem. What happens in my application, when changing state, any memory problem or any other...
When the sleep/wake button is pressed, your application is sent to the background/changes mode to inactive. Check UIApplicationDelegate documentation applicationWillResignActive: and applicationDidEnterBackground:. When this happens there are restrictions about your application doing background operations, which are described within the documentation.