Application Restarted on Orientation change,How To save State? - android-emulator

I am building an app that changes every time the user clicks on a button the text view changes and moves to another part of the activity...When the orientation is changed the view is destroyed and recreated.
How can i save it to be the same as it was before, and save the last button the user clicked, to put them back where they were.

You can subscribe the Sensor Events that will let you know when the orientation has been changed. When this event is fired fire all your data into a DataState object and send back to the Activity. When the activity is started up again it will check to see if the DataState object is not null and you can restore the state from that!
I am aware this answer is a bit jumbled but it is possible to do this. I'm just not sure on the specifics.
http://developer.android.com/reference/android/hardware/SensorManager.html

Related

NSNotificationCenter re-executes notifications

I am using NSNotificationCenter to set a variable shared when a notification comes in across 3 TableViewControllers (all subclassed from the same class).
All works fine except for the following scenario:
The user is in the main view (the view that actually processes/responds to notifications) when the app goes to the background (e.g. user presses Home button). If a notification comes in and the user launches the app either from the icon or from the alert the app behaves as if it needs to process not only the last notification but also all the notification up-to-now.
For example: let;'s assume that the app processed 5 notification already. The app moved to the background and then a notification came in. The user launches the app from the icon which causes the app to move to the foreground. The app will trigger 1 action per notification.
This is not the case if the user was in any other screen before the app moved to background. However, if the user moved to the main view, the notification was processed and the app moved to background again, on the next notification, the app will process 7 notifications (the 5 we had + 1 previous + 1 current).
While conceptually it seems as if my app is spawning n-observers, it is not clear to me why. I set an observer in the base class in viewWillAppear and remove in viewWillDisappear (also tried a deinit block - no change in behaviour).
Any idea why the observer accumulate all notifications? Is this the normal behaviour for an observer? If so, how do I remove 'observed and processed' messages?
The solution to my problem was to remove the observer before adding it.
The problem itself came down to the fact that the order of cleanup (i.e. add observer in viewWillAppear, remove observer viewWillDisappear) did not match my expectations. The result was that the observer was being added again and again but the corresponding 'remove' did not actually 'happen'.
I guess it was due to the fact that adding/removing observers took place in the base class (and not sub-class).

Multiple simultaneous UILocalNotifications

If I have two (or more) UILcalNotifications that fire more or less on the same time and the application is active in the background, I found out that:
Two alerts are shown simultaneously to the user, one covering the other.
When the user touches "View" on the top alert, the alert is removed from the screen, didReceiveLocalNotification is called for this notification, and the application enters the foreground.
As soon as the top alert is removed from the screen (and the application is already in the foreground), the user sees the alert that was under it.
However, if the user touches "View" for this alert as well, nothing happens. didReceiveLocalNotification is not called for the second notification, and the application has no way of knowing that the user wanted to view this notification as well.
If the application happens to be in the foreground when the two notifications fire, there's no problem - didReceiveLocalNotification is called for both, one after the other (no alert is shown in this case).
Is there a way to get some notification for both "View" confirmations in the above case? Am I doing something wrong?
Actually the previous notification do not call the didReceiveNotification delegate method (or any other method if two or more notification pops while app in background). But you can track that previous notification as your need.
For example, if you have a app that send data to the server on clicking view of notification then on daily basis save the data into a plist (and edit it daily) that data has been sent or not by "Yes" or "No" so while a notification comes the app checks into the plist that the data for previous alarms have been sent or not. If no then it will send it at that time. So this is just an single way.
Alert view needs to have it's delegate set (typically to self) to receive events.

Deferentiating the push notification handler when application is foreground and background

It is said that (correct me if I'm wrong) if the application is in the foreground we have to handle push notifications in the "didReceiveRemoteNotification" and if the application is in the background using "didFinishLaunchingWithOptions" when user taps the "view" button of the app. As I dont have a phone to test I want to know whether I am handling this properly.
1) What will be invoked when I taps on the "View" button in the push notification?
2) Let say I am running the application in the foreground and push notification receives at the same time. Will I be given the push notification alert? If so what will happen if the user click on the View button?
3) In this thread How to handle push notifications if the application is already running? it says:
"alert" key will not be there directly under the userInfo dictionary, you need to get another dictionary with name "aps" and then get the "alert" or "body" from "aps" dictionary"
Is this true?
4) I need to push to a certain view when the user clicks on the View button. Hence do I need to handle that code in both methods?
Thank you
There's a nice rundown of the methods invoked by a push notification in this Apple vid: http://developer.apple.com/videos/iphone/#video-advanced-pushnotification - make sure you visit download the full version in iTunes.
This direct link might work: http://developer.apple.com/itunes/?destination=adc.apple.com.3391495696.03391495702.3416205190?i=1378617410
Either way, the general idea is that if your app isn't in the foreground, tapping your view button will trigger didFinishLaunchingWithOptions, and if it is the foreground app, you'll get the didReceiveRemoteNotification.
I don't think you'll get the alert. The method didReceiveRemoteNotification will be called, and it'll be up to you to show a UIAlert if you want.
Yes - that's true.
Yes, but I think you can simplify this by creating a third method specifically designed to handle your view. You can call this from both didFinishLaunching (only if it launched via a notification), and didReceiveRemoteNotification. This way, if your app needs to be launched, you can have time to do any other setup you might need to do for the app to work right out of the get-go (load saved data, init tabbar controllers or anything else like that).
Best of luck

how to show the same view when the app is reopened

I have a tableview which shows a webview on clicking some row in the table, which in turn picks up the data from an sqlite. if a user closes the app by pressing home key while viewing a description in webview and reopens it after sometimes, I should be making the user to see the same screen. how to show the same view again ? What is the efficient way ?
Well I think the easiest way is to store the state of the application in NSUserDefaults. There is a delegate method on UIApplication called:
- (void)applicationWillTerminate:(UIApplication *)application
This delegate method gets called when the user quits the app. This is the time where you can save the state of your application off to the NSUserDefaults. But be aware that you cannot do time intensive stuff there. If you do, you get killed by the OS.
In your case why not simply store the row the user picked in NSUserDefaults and then check in
-(void)applicationDidFinishLaunching:(UIApplication *)application
if there is a saved row and restore the screen approbiately.
The easiest way is to write a string to a file in your documents directory that holds your state information in some easily parsed format. For example "screen_name:database_id". Or build a JSON string and parse it with JSON.
Everytime you switch screens, rewrite the file. When you open next time, read the file, parse it, and show the appropriate screen in your app.

How to Recreate the Last State of an App the Next Time it is Launched?

I have 2 views. When I'm on the 1st view, I have a textfield. I enter some text in the textfield. I exit the app, launch some another application and then return to my application. I want to maintain state of the textfield and that view. Likewise, if I quit with the 2nd view active, when I return to the app I need to return the view to the state it was in when the app quit.
How do I save the information about the state of my app when it last quit so I can recreate it upon the next launch?
Use the NSUserDefaults to write the value and selected view index to the user's preferences.
Apple's DrillDownSave sample may be helpful