applicationDidEnterBackground & applicationWillResignActive are not being called - iphone

I am creating a simple application which perform some task on main thread. I am printing process in NSLog so I can understand that my process is running or not.
Now when I press home button without starting the process (Process will be start when I tap on a button) application enters in background and my both of methods applicationDidEnterBackground & applicationWillResignActive are being called.
But when I first tap on my button and process starts on main thread after that if I press home button none of these two method being called. So my application can't know that app entered in background or not.
Even after that when I again active the app it shows me a black screen with status bar only.
Why this is happening?
Why app not entering in background?
Why apple's methods not being called?
Is there a way to solve it?
UPDATE
Here is my appdelegate class code
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
All methods have no implementation.
Thanks in advance.

I am creating a simple application which perform some task on main thread.
Don't perform long-running operations on the main thread.
The delegate callbacks happen on the main thread. If the main thread is busy, then the callbacks won't happen until you return to the "run loop".
When foregrounding your app, the OS actually displays a screenshot if available, falling back to the launch image (Default.png). The screenshot is taken after -applicationDidEnterBackground: returns, which allows you to customize what gets saved (you might want to do this for security reasons, or to hide UI elements which might not make sense to show when relaunching e.g. a countdown timer).
The black screen is probably because your app has no launch image. If your app takes more than about 10 seconds to enter the background (and it does, since the main thread is blocked), it gets killed. Except the debugger is attached and catches SIGKILL, so it's easy to miss unless you're watching Xcode.

there are some cases
if UIApplicationExitsOnSuspend key set to true in your app's Info.plist, the applicationWillResignActive method is not called when the user hits the home button. and may b some thing other. check keys here Apple keys and see if something new you added to plist. and there is no other case that you say your delegate method not calling. it may also some time due to project in appropriate behavior. try cleaning your project and rebuild.

this is going to sound strange but for those it helps. I had the same issue and cleaned my project and then it started working again.

Related

How to set First viewController Screen as a Default opening screen when ever open a app

Hi I am new to iPhone development, I am developing a TabBar app it's contains 5 tabbar Items.
When I am close app at third TabBar viewController screen and again when I am opening app that time same third TabBar viewController screen only opening.
How to set App first screen as a default App opening Screen in iphone.
I need to set first screen as a default opening screen when ever open a app.
Could you please share your ideas here..
When you close the app, it doesn't KILL it, it just suspends it. Unless you set the option to kill it when you close it.
I suggest you read Apple Documentation For Application Flow
And possibly read the other documents Apple have provided for you there if you are new to all of this.
In your application's Info.plist, add a boolean key UIApplicationExitsOnSuspend with the value YES.
This will always exit the app (instead of running in background) when you close it. On opening app again, first view controller will appear instead of where you left it last time.
This is because you are setting your application to be run in background, so whenever you close your app the app will run in background and resume on startUp.
so just do one thing
Go to your info.plist and set value for
Application does not run in background to YES
If application is killed by default it loads all settings in Application Delegate method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Here you can modify your root view controller, which I presume is some kind of tab bar view controller.
So there is probably some code in that method which defines which tab to load.
However if you want to show specific tab after application enters in the foreground use Application delegate method:
- (void)applicationWillEnterForeground:(UIApplication *)application
Or
- (void)applicationDidBecomeActive:(UIApplication *)application
When exiting the app by for example pressing the home button on the iPhone the app is not terminated. Instead the execution is suspended but the app will remain in memory until iOS decides to remove it. This means that when you enter the app again it will be in the same state you left it. You can see if the app is still in memory by doubletapping the home button.
When your app becomes active the method applicationDidBecomeActive: in your UIApplicationDelegate will be called. You can implement that method to set the state you want your app to be in when it is restored.
I would not recommend explicitly killing the app when it is suspended. That forces the app to be reloaded each time it is started which will degrade performance.
For more information see Apple's documentation on app states.

how to restart a application without going to recent worked page ,when we exit in ipad

I had installed my app on ipad(ios 4) , and i was navigating through the pages of my app.whenever i close the app and re open the app it goes back to the recent page which i was working on.
But i want it to restart the app from first page,whenever i exit and reopen .As it is working on iphone(ios 3.1.3),Can any one suggest how to do that on ipad.
thanks in advance
I think you can do that easily. You need to set info.plist key "Application does not run in background" to YES and it will no longer to run in background.
When application enters background the delegate method appDidEnterBackground is called and when resumed the method willEnterForeGround will be called.
You can handle your application behaviour in these methods.
For more info you can refer to this link which explains the concepts beautifully.
On iOS 4 the app does not exit it goes to background. And when you tap on the icon the app resumes from where you left it.
If you want to reset something when it goes in background you need to implement custom code in appDelegate methods:
- (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 inactive state; here you can undo many of the changes made on entering the background.
*/
}
You need to set UIApplicationExitsOnSuspend to yes in your info.plist
Which methods are called when user presses home button and double presses it again to return
here you can find answer to event on clicking on home button
interesting article
Find your info.plist file in resources and add the key:
"Application does not run in background" and set the value to 1.

Using the applicationWillTerminate method doesn't seem to be working

I am wanting to save some app state data for when the user is quitting the app. I am thinking that I should be using the applicationWillTerminate method inside of my appDelegate, but when I try and do anything there, it's not doing anything:
- (void) applicationWillTerminate:(UIApplication *)application {
NSLog(#"test");
}
When I run my app, and do some stuff, and hit the home button to quit the app, nothing comes over the console...
Do I have to implement the applicationWillTerminate method in the appDelegate? The user of my app will most likely be in a Reader view when they leave, is there anyway to implement the app will close method there?
see this link iPhone simulator and applicationWillTerminate() for older iOS versions;
but remember the Home button does not necessarily terminate the application is ios4... you should use applicationDidEnterBackground but would suggest both places applicationWillTerminate and applicationDidEnterBackground
Adding the UIApplicationExitsOnSuspend key to your application’s Info.plist file and setting its value to YES will make you application quit when you hit the home button, even on iOS4
In iOS 4, the applicationWillTerminate: method is only called if you opt out of the background execution model. In the standard iOS 4 application lifecycle, applications are suspended (not terminated) when the home button is pressed. Your application delegate will receive an applicationWillResignActive: message followed by an applicationDidEnterBackground: message. The applicationDidEnterBackground: method is a good place to save user data.

Multitasking and termination

Is there any possiblity for reacting to the event that a user kills your app via the multitasking bar if it has moved to the background? According to my observations, applicationWillTerminate: does NOT get called.
It seems to me that there is no possiblity for cleaning up before quitting in this case.
If an app needs to do any cleanup or shutdown, under iOS 4.x it should do this when the app's suspend delegate gets called, just before the app gets sent to the background, since there is no guarantee that the app will ever get any run time again, either due to user action or memory cleanup.
If the app's Deployment Target also includes iPhone OS 3.x, then it should also do cleanup in its terminate delegate, as that will get called instead of suspend.
It should get called. Are you depending on NSLog to tell you when it does get called? When an app goes into the inactive state by pressing the home button then any further NSLogs are not printed to the console. You could try showing a small UIAlertView to see of it does get called instead.

iPhone. Shouldn't hitting the home button cause UIApplicationDelegate's dealloc to be called

I have put NSLogs in all my classes including my UIApplicationDelegate subclass. I am curious - and a bit nervous - about why I am not seeing them echo anything when I press the home button. I am running in the XCode simulator.
Since iPhone/iPad runs a single app at a time, doesn't hitting the home button discard all traces of the running app?
Thanks,
Doug
Chuck is correct, the dealloc's don't matter at that point. If you want to do something as your app is expiring, implement this in your app delegate class:
- (void)applicationWillTerminate:(UIApplication *)application {
// goodbye...
}
When an app is terminated, its memory is simply freed. Dealloc is not called, it does not pass go or collect $200. This is normal and intended.
You should implement -(void)applicationWillTerminate:(UIApplication*)application{
}
this method will get called when user hit home button.
You might also want to think about the additional multitasking support that has been announced. Obviously the details are still under NDA, but pushing Home does not necessarily stop your app.
And think about the various circumstances where your app may already be terminated quickly (e.g. if a call comes in and the system runs out of RAM). Do your cleanup with the application lifecycle messages.