the destroyed fragment onActivityResult is called instead of the re-started fragment - onactivityresult

Having problem that the onActivityResult in the destroyed fragment is still called.
The OS could destroy the activity for some reason (i.e. activity goes to background and with low memory, not only the configuration change).
In my test case it has a activity/fragment to start the camera activity, when the camera view is up the os may destroy the lunching fragment (one easier way to make it happens is to check the "Don't keep activities" in Developer option settings (but you don't have to, the destroy may happen for some reasons the os decides to do so).
It is similar to the problem of Android: Activity getting Destroyed after calling Camera Intent
But the here difference is The Fragment F1 opened the camera activity. F1's onDestroy is called by os. After took photo from camera activity and back to the lunching fragment the os recreates it but it is a new instance F2 of the fragment.
The trace shows the onActivityResult on fragment instance F1 is still get called with the new photo data, but the fragment instance F2's onActivityResult is not called.
Is this common problem when the destroy/recreate by os? How to catch the callback from the opened activity (like camera in this case) and pass the data to the os newly created fragment instance?
Thanks!

I think the problem is that when os re-create the activity at onCreate(Bundle savedInstanceState), we should check savedInstanceState to not to recreate a extra new Fragment instance ourself. Let the os does the re-creation of the Fragment instance. Then the onActivityResult() is called on the proper Fragment instance.

Related

Unity Networking: how to sync animations on child object?

I'm making a multiplayer but I got stuck at syncing animations. My current player hierarchy is following:
The problem is that Marine object uses IK to aim up/down which means that I use OnAnimatorIK function. In order to use OnAnimatorIK, the Animator component must be attached to the same gameobject as the script using OnAnimationIK. This means I must attach the Animator to the Marine gameobject. If I do this everything works fine (if not trying to Network animations).
Now, if I attach Network Animator to the Player object, animations will not update remotely. When I try to attach the Network Animator to the Marine object, Network Identity component is automatically added too and it can not be removed. If I try to build with Network Identity component attached to Marine object I get error saying "You can only have one Network Identity attached and it must be attached to the root object". This means I should move my Animator component as well as the PlayerIKHandler script to the Player object to get rid of the duplicate Network Identity component.
If I do move the Animator component to the Player object it breaks my animations (model stands in T-pose) and I don't see anything animating even locally.
Clearly I'm doing something wrong I just don't know why. How could I sync animation using animator on child object OR how can I animate the model if the animator component is attached to the Player (just a dummy Capsule with scripts on it).

Reset users default app brightness in applicationDidEnterBackground with swift

I've been trying to reset the user's default device brightness in the app delegate method: applicationDidEnterBackground by using this code:
UIScreen.mainScreen().brightness = screenBrightness!
The code gets called but the brightness is not reset. Anyone know how to get this working using Swift (not obj-c)
This code looks correct, but note that it will not work in the simulator, it will only take effect on a physical device.
Also, ensure that the value of screenBrightness is between 0 and 1.
You will likely not be able to change the screen brightness while in the background. From Apple's documentation:
Brightness changes made by an app remain in effect only while the app
is active. The system restores the user-supplied brightness setting at
appropriate times when your app is not in the foreground.
You need to change the brightness in the AppDelegate. For some reason it doesn't work properly when triggered in a view controller from a notification (willResignActiveNotification, didEnterBackgroundNotification, willTerminateNotification). Although, you can use AppDelegate methods for that:
func applicationWillResignActive(_ application: UIApplication) {
UIScreen.main.brightness = 0.5
}

Red audio recording status bar "flashes" while app in *foreground*

There are many questions (here, here) regarding the double height red audio recording status bar, but all of them reference flashes when the app resigns into the background. I'm getting a flash, I'm assuming from an AVCaptureSession setup, while the app is in foreground.
Has anyone experienced this before?
You have to remove the audio input from the AVCaptureSession
[self.captureSession removeInput:audioIn];
in which the audioIn is the AVCaptureDeviceInputobject, that is initialised in the init method.
Explanation:
You get a flash because of the transition. When you go from view A to view B, and the object was allocated in view A. You get a flash because when the view B is presented, and view A still hasn’t deallocated the object. So it is still being used on “background” by view A. It’s the same thing when you are on a call and open an app while you are on a call.

Make application startup with size and position it was when it was closed

I'm currently creating a application that, when it reopens need to have the same size and screen position as just before it was closed.
I hope that it is just a checkmark in interface builder that i haven't noticed.
Thanks! :-)
You should implement Application Persistence.
Read more here.
When a user logs out, Lion offers them the option to restore all open apps to their current state when logging back in. To support this feature in your app you must determine for each window whether its state should be preserved using the -setRestorable: method. Cocoa will then take care of saving the state (size, position, etc.) of your windows and their associated window controllers, giving you the option to write out additional state information of custom objects associated with the windows.
To restore your application’s state when it is relaunched, every window must specify a so-called restoration class through the +restoreWindowWithIdentifier:state:completionHandler: class method (defined in the NSWindowRestoration protocol). The restoration class is then responsible for instantiating the window and its associated objects (such as the window controller). See the User Interface Preservation topic in the Mac OS X Application Programming Guide for a step by step guide.
Close to a checkbox. Set the window's frame auto-save name. That's a key naming a value in the app's preferences (which is managed by NSWindow) under which the window's frame is stored and retrieved.
Store size and position in NSUserDefaults.For example you store a cgpoint in Nsuserdefaults
as follows
CGPoint *point=CGPointMake(34,67);
NSUserDefaults* def=[NSUserDefaults standardUserDefaults];
NSString* mypointstr=NSStringFromCGPoint(point);
[def SetObject:mypointstr:forkey:#"mypoint"];
to get this next time the app starts
NSString* myprevstr=[def Objectforkey:#"mypoint"]
CGPoint* point=CGPointFromString(myprevstr);

Stop Device orientation

While running my app I get errors which indicate that rotation notifications are being sent and crashing the app. Is there a way I can stop all rotation events from being sent to see if I can stop the crashes. Also is there a good overview of how these events should be handled. Do I have to handle them separately in in controllers or in all views. For the moment I would be quite happy to lock in portrait.
I have tried calling [[UIDevice currentDevice] endGenerating Device OrientationNotifications];
but I am still getting device Orientation DidChange events unrecognized selector sent to instance
You can't stop rotation notifications, nor should you try to.
The notifications aren't causing your crash, you must have some bad code in there.
Paste your exact error from the console and the code for the view controller where it's happening. My guess is that you've written a rotation handler method that doesn't work, possibly without realising it.
You can lock in portrait by creating a view controller base class that has this method, then using it as the superclass for all your other view controllers:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}

Resources