Before getting started with Android development there are some basic prerequisites that you should be aware of
- Object-oriented programming concepts. Android makes heavy use of this.
- Some experience with Java.
- Experience with Eclipse development Environment.
- Basic knowledge of Android.
Android Architecture
- Android runs on top of Linux kernel.
- Android uses a virtual machine called Dalvik Virtual Machine . This is specially optimized for mobile devices.
- Android has an integrated browser based on open source WebKit engine.
- Android has OpenGL ES which is embedded version of OpenGL(2D/3D graphics).
- SQLite database for structured data storage.
Android versions
- Cupcake (1.5)
- Donut (1.6)
- Éclair (2.0/2.1)
- Froyo (2.2)
- Gingerbread (2.3)
- Honeycomb (3.0/3.1/3.2)
- Ice Cream sandwich (4.0)
- Jelly Bean (4.1/4.2/4.3)
- Kitkat (4.4)
- Lollipop (5.0/5.0.2)
Application Fundamentals
- Applications are written in Java programming language.
- You compile applications into Android package file(.apk files) which you can then distribute.
- Each application run in its own sandbox protected and isolated from other. They also run in their own Linux process.
- Application consists of Components, a Manifest file and resources.
Android Components
- Activities
- Represents a single screen with a user interface.
- An application consists of multiple Activities.
- When new Activity starts, the old one is pushed onto stack which user can navigate by pressing back key.
- Can be built with xml files or directly by Java.
- Services
- Used to perform long running operations in the background.
- No interface.
- For example playing music. No matter what you are doing music keeps playing once started.
- Can be bound to other application.
- Content Providers
- Used to store and retrieve data and make it accessible to all the devices.
- By default there is no way to share data across different applications.
- Exposes public URI to others application.
- Uses database model.
- Example Contacts, Media etc.
- Broadcast receivers
- Responds to system wide broadcast announcements
- Example when screen turns off Android sends broadcast which your application can listen to. Other example is when battery gets low.
- You can also broadcast your own messages which other applications can listen to.
- No user interface though they can be used to create status bar notifications.
Android Manifest File
- Name must be AndroidManifest.xml and must be in Applications root directory.
- Gives Android System information about the Application.
- Describes components(mentioned above like Activities) used in Application.
- Declares permission required to run the application. If you must have notices when you try to install any app from the market store you have to give some permissions. These are given here.
- Declares minimum API level. If this is set only people with Android running version more than the API level will be able to view the application on the app store.
Android Lifecycle
Android Lifecycle Methods
- onCreate() :This methods is called when activity is created.
- First call will be to super.onCreate() so that android will do some of it's own initializations.
- You can set the activity's content view by calling method setContentView().
- You can get references to various UI components like EditText, Button etc by calling findViewById() method and then attach various listeners to it. Eg you can set onClickListener for button.
- onRestart() : This method will be called when activity is stopped and is about to start again. If there is any logic that you need to execute on activity restart you should add it here.
- onStart() : [Visible only behavior] This method is called when the activity is about to start. In this method you should typically add logic to load persistent application state like reading from database.
- onResume() : [Foreground only behavior ] This method is called when activity is visible and is about to start interacting with the user. In this method you should do tasks related to activity coming to foreground. For eg. Starting a playback music or starting some animation etc.
- onPause() : Opposite of onRsume(). This method is called when activity is about to loose focus. In this method you should do things like stopping animation or stopping playback music... handle things you started in onResume() method. You should also save any data that you need to persist like storing user entered data in database.
- onStop() : This is when activity is no longer visible to the user. In this method you should typically cache your activity state in case activity is killed and needs is restarted later.
- Note : If activity is killed by Android OS then onStop() is not called. So saving any persistent data should be done in onPause() method.
- onDestroy() : This method is called when activity is about to be destroyed. Typical things to do here is release resource hold by your application.
- Note : Again if activity is killed by Android OS then onDestroy() is not called. So saving any persistent data should be done in onPause() method.