Some tips while developing Android applications
- If you plan to use a class derived from the Application object for storing application wide data (for sharing it across Activities) remember to give it a name in the manifest file. If a name is not specified in the manifest, the return value of getApplication() cannot be cast to our application object.
- When providing a name for the main application in the manifest file, use the actual string – till Android 2.1. Adroid 2.2 accept a reference to a string (which is then defined in strings.xml).
- Ensure that you ‘nullify’ all objects used in the application, so that the application will not remain in memory for longer than necessary.
- Trap the ‘back’ key using the onKeyEvent and perform application cleanup explicitly, instead of relying on the Android system to do so.
- If the application does not have a visible exit button, trap the ‘menu’ key and provide an option to ‘exit’. This will also allow you cleanly exit the application, instead of replying on the Android system.
- Trap both keys – ‘menu’ and ‘back’ for good application behaviour.
- To exit the application, use ‘finish()’, instead of relying on the system.