The advent of android 10 years back brought with it a huge wave of job opportunities for mobile developers. With 75% of the mobile market share today owned by android, it is no surprise that companies all around the globe want to have their android application out in the market. Hence, Android Developer jobs are in high demand, which is well reflected by the handsome pay that comes with it. Today, we look at the top 10 android interview questions that will help you crack your interview with ease.
1) Android Activity/fragment Lifecycle
Android developer interviews never start without the lifecycle question. As basic as it sounds, if you don’t know the activity lifecycle, then you don’t really understand application development in android. A very commonly asked question is “Briefly explain the states in the activity lifecycle”. We will look at the activity and fragment lifecycle in parallel. A simple rule of thumb is:
Fragment lifecycle is always one step behind the activity lifecycle
This makes sense because the fragment cannot live outside an activity. So the onCreate for the fragment has to be called only after the onCreate for its parent activity is called. The figure below shows the activity and fragment lifecycle:
2) Components of android application
Components are the basic building blocks of the application. Every android app has 4 components:
- Broadcast Receivers
- Content Providers
Activities are the user interface screens in an android application. The users interact with the application via the activity.
Services are used to perform background tasks, such as as a music player, or downloading files from the internet. It does not have a user interface. Read the android documentation for more details.
Broadcast Receivers allow the android app to receive events. Let’s say your application wants to know when the phone’s internet connectivity changes. Your application can register to android.net.conn.CONNECTIVITY_CHANGE and receive changes to the network. Click here to read in detail from the android official documentation.
Content Providers allow you to manage the data in your app. It can be from you sqlite database, files or even from the cloud. It also provides an interface for other apps to query or modify the data (with permission of course!).
3) View and Viewgroups
Views in android are the basic UI elements that the user directly views and interacts with, such as Button, TextView, EditText, ImageView etc.
ViewGroups are containers for these views. They help you organize multiple views inside them. They are the layouts for your applications (hence names ending in Layout). Examples of viewgroups are LinearLayout, RelativeLayout, FrameLayout and the newly introduced ConstraintLayout (which is pretty awesome!).
4) What is AndroidManifest.xml?
Every android application must have an AndroidManfest.xml file. This file contains essential information about your application. It contains the permissions that are used by you application ( which is used by the google play store to tell your users what permissions you use). It also contains information about the components(activities, services, broadcast receivers, content providers) of your application. Your application package name is also contained here.
5) What is gradle and why is it used in android?
Gradle, in simple words is a build system. It is not exclusive to android. It was introduced in 2007 and has been popularly used. A build system allows you to write scripts(using groovy in case of gradle) and automate your build process. This is exactly what the build.gradle does in your android application. Most of you would have used the build.gradle file to update your version number or change the application ID but it does much much more than that! We won’t discuss that but you can read about it here: Writing scripts in gradle.
The reason android (or google?) adopted it is because it is a very popular build system and makes life easier for the developers. If you have developed android applications in Eclipse with ADT, you know the pain!
Intents are message carriers in android. You use them to send data or request action between your app components. As an example, let’s say we have a simple application with 2 activities. The first activity takes user input from the EditText and the second activity displays it. How would you send the text from the first activity to the second? You put it inside the intent object. When you call the second activity from the first, you pass this intent object along.
7) ListView, ViewHolder and RecyclerView
These 3 require separate articles of their own, but i’ll try to briefly summarize over the important concepts. ListView is used to display a list of items(can be simple strings or custom views). But imagine you have 100,000 items, your basic ListView would create a 100,000 views. But unfortunately(or fortunately?) the android phone screen isn’t long enough to show all of them at once. Let’s say your phone can show 10 items at a time. Then it’s a waste of memory to create a view for the rest of the items and this makes your list view laggy.
This is where the ViewHolder does its magic! Instead of creating 100,000 views, you only create the 10 views and display it to the user. But you hold a reference to each of these 10 views. When the user scrolls down, you update the same views with new data. The ViewHolder essentially holds these views and this pattern of using ViewHolder with the ListView is called the ViewHolder pattern. RecyclerView is basically a ListView which implements the ViewHolder pattern.(It does a lot more than that but for interview basics, this should do). Before RecyclerView was introduced, you would need to implement the ViewHolder pattern yourself.
8) Is it ok to overload a constructor in your fragment?
No! The only constructor your fragment should have is the default constructor (the one without arguments). So then how do you pass parameters to the fragment? By passing Bundle to fragment.setArguments(Bundle) and using it in the fragment onCreate by calling getArguements(). This is very clearly explained in the fragment documentation. So please do read it. There is a reason why you shouldn’t overload your fragment constructor. Let’s say the android system destroys your fragment (because of low memory). When the user tries to resume the app, the system tries to recreate the fragment. It does not call the overloaded constructor. So if you are initializing variables in your overloaded constructor, they will remain null when the fragment is reconstructed and it will crash!
That’s it for this article! Leave your questions in the comment section below and Good luck for your interview!