Showing posts with label bundle. Show all posts
Showing posts with label bundle. Show all posts

Sunday, December 08, 2013

Android Tip - Saving Instance State

One of the characteristics of an activity that it is automatically destroyed and recreated whenever there is a change in device orientation. As such, it is important that you take note of this so that you don’t lose any data when the user accidentally rotates the device.


When an activity is rotated, the activity will fire the onSaveInstanceState() method before the activity is destroyed:

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        //---save whatever you need to persist—
        outState.putString("str", "This is a string!");
    }

You can make use of the Bundle object to save the state of your activity.

Do not confuse the onSaveInstanceState() method with the onPause() and onStop() methods, as:
·       The onPause() and onStop() methods are always called when an activity is sent to the background or is being destroyed
·       When an activity is being destroyed, the onSaveInstanceState() method will not be called; it will however be called when the activity is sent to the background
·       The onSaveInstanceState() method will be called when it is being killed by the OS due to a memory crunch

When the activity is created, you can use the onRestoreInstanceState() method to restore the previous saved state of the activity:  

    @Override
    public void onRestoreInstanceState(
    Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        //---retrieve the information persisted earlier---
        String s = savedInstanceState.getString("str");
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }


While usually you can also use the onCreate() method to restore the activity state, the onRestoreInstanceState() method is another method for you to restore the activity state.

Friday, July 19, 2013

Xamarin.iOS Tip - Copy File in Bundle to Documents Folder

Very often, you need to attach resources to your application so that you can use them during runtime. For example, you might have an empty database created during development time that you want to use to store the locations coordinates collected when the user is using your application. In order to use that database, when the application is first run on the device, you need to copy it from the application bundle onto the application’s Documents folder.

The following method allows you to specify a file to copy from the application bundle onto the application’s Documents folder:

        private void CopyFileInBundleToDocumentsFolder(
        String filename)
        {
            //---path to Documents folder---
            var documentsFolderPath =
                Environment.GetFolderPath(
                    Environment.SpecialFolder.MyDocuments);

            //---destination path for file in the Documents
            // folder---
            var destinationPath =
                Path.Combine(documentsFolderPath, filename);

            //---path of source file---
            var sourcePath = 
                Path.Combine(NSBundle.MainBundle.BundlePath,
                filename);

            //---print for verfications---
            Console.WriteLine(destinationPath);
            Console.WriteLine(sourcePath);

            try {
                //---copy only if file does not exist---
                if (!File.Exists(destinationPath))
                {
                    File.Copy(sourcePath, destinationPath);
                }  else {
                    Console.WriteLine("File already exists");
                }
            }  catch (Exception e) {
                Console.WriteLine(e.Message);
            }
        }

You typically call this method when the application is first launched:

        public override bool FinishedLaunching (
        UIApplication app, NSDictionary options)
        {
            window = new UIWindow
                (UIScreen.MainScreen.Bounds);
           
            viewController = new
                BundledResourcesViewController ();
            window.RootViewController = viewController;
            window.MakeKeyAndVisible ();

            //---copy file in bundle to documents folder---
            CopyFileInBundleToDocumentsFolder("MyDB.sql");

            return true;

        }