简体   繁体   中英

Xamarin iOS method to override to save data when app navigates to background, closes or navigates to different screen

I am very new to mobile app development and xamarin iOS. I have a view controller which has some UIImageViews, I am setting the UIImageview with an image. What I would like to know is what event to hook onto to save the image when

User Navigates to a different view controller App goes into the background App is closed

I have been looking into xamarin lifecycle and thinking maybe the ViewDidUnload will be a good one. Any thoughts?

Please forgive my ignorance guys.

ViewDidUnload is deprecated.

You should use viewWillDisappear to save data/image.

This method will call whenever you will navigate to different controller. But this will not cover, when you your app will go into background or killed.

UIApplication's applicationDidEnterBackground will be called after your app will enter into background state. and applicationWillTerminate will be called before your app will terminate.

You can add observer in your view to monitor these events.

example of observer for App enter into background:

 NSObject notificationObserver = NSNotificationCenter.DefaultCenter.AddObserver (UIApplication.applicationDidEnterBackground, SaveData);

    private void SaveData(NSNotification a_notification)
    {
        // you can save image or any other data  
    }

You can implement similar for applicationWillTerminate .

Darshana's answer is correct.

I would like to add on a different way of observing those events:

UIApplication.Notifications.ObserveDidEnterBackground(HandleNotification);

public void HandleNotification(object sender, NSNotificationEventArgs e)
{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM