Android interview questions and answers

Q. Explain android activity life cycle

Activity life cycle having 7 methods.

  • onCreate – This method is called when the activity is created.
  • onStartThis method is called when the activity is visible to user.
  • onResumeThis method is called when user start interacting with activity.
  • onPauseThis method is called when another activity comes to foreground.
  • onStopThis method is called when the activity is no longer visible to user.
  • onRestartThis method is called when activity stops and start again.
  • onDestroyThis  method is called when activity is destroyed completely.

Q. Explain android services

  • It is used to perform long running operations in background.
  • Services will run in background indefinitely even if the application is destroyed.
  • It doesn’t have any UI.
  • Services can be used to implement task like playing music, handling content providers etc.

Read more about services 

Q. Explain android Architecture

Android architecture is having 5 categories

  • Linux Kernel

This is the root of the android architecture which is responsible for power management,device drivers , memory management and accessing resources.

  • Native Libraries

This is layer above linux kernel. This layer consist of libraries such as Webkit,OpenGL,Sqlite,Media etc.

  • Android Runtime

This will be in same layer as native libraries. Android consist of DVM(Dalvik Virtual Machine) which is responsible for running android applications. DVM is same as JVM, optimised for mobile devices which will take less memory and fast performance.

  • Application Framework Layer

This layer provides high level services to android applications. This layer includes activity managers, content providers, telephony manager,location manager and resource manager and it provides a lot of classes and interfaces for application development.

  • Application

This is in the layer above application framework layer. This layer will be having all the applications like contacts,home,browsers etc

Q. Explain android application components

Application components are the essential building blocks of android application. Components are activities, services, broadcast receivers, content providers, fragments, views, layouts , intents, resources ,manifest.

Q. Types of intents in android

  • Implicit Intents

This type of intent will not specify the component. So the intent provides information of available components provided by the system. Intent sharing to gmail app is an example.

  • Explicit Intents

This type of intent will specify the component like another class name. Opening one activity from another and passing data from one activity to another are the examples of explicit intents

Q. Explain Broadcast Receivers

Broadcast receivers responds to events or intents from other applications or by the system itself. All registered receivers for an event will be notified by android runtime once the event happens.

Steps to implement

  • Create a class which extends BroadcastReceiver class . This class will be having an override method onReceived.
  • This will be method which will get called once broadcast message received. You will get the information in intents which will be an argument of this method
  • Register the broadcast receiver in AndroidMainfest.xml file. You can also register a receiver dynamically using Context.registerReceiver() method.

Q. What is Async task

Android AsyncTask is a class provided by Android which gives the ability to perform heavy tasks in the background and keep the UI thread light. With the help of assyncTask you can keep the UI thread of your application responsive always.

The basic methods used in assync task are

  • doInBackground – This method will execute the task which should be done in background and notify UI thread regarding task completion by using return statement
  • onPreExecute –  This method will process the task which should be done before background task starts.
  • onPostExecute – This method will execute after the completion of doInBackground. In this method you can perform UI operations
  • onProgressUpdate – This method can receive the progress updates from the doInBackground method.

Read more about Async task

Q. Android Fragments

  • A fragment is a part of activity.
  • In android we can display multiple layout in single activity with the help of fragments.
  • Fragment is having its own life cycle.
  • Same fragment can be reused in multiple activities.
  • You can add or remove fragments in an activity while the activity is running.

It is good to explain more about fragment including implementation when you face this question. You can see an example of fragment implementation here

Q. Sqlite database

  • Sqlite is database which in embedded in android by default.
  • It is  an opensource database which we can use to create,delete and edit data
  • For implementing an sqlite database you need to have an SqliteOpenHelper class which will implement onCreate() and onUpgrade() methods.
  • The onCreate() method is called when the database is created for first time.
  • If you want to make some changes in your database structure like creating new table or altering existing table you need to make that changes in your onUpgrade() method.

Read more about sqlite database here

Q. Types of  broadcasts in android

Normal Broadcasts
  • send broadcast using SendBroadcast() method.
  • If there are multiple receivers for the broadcast then android will start receivers in any order.
  • The order can be random,simultaneous,or one by one.
Ordered Broadcasts
  • Send broadcast using SendOrderedBroadcast() method.
  • Programmer has control over the order in which receivers are executed.
  • Programmer can prioritise the order by setting priority in intent-filter tag .
  • More priority number has higher priority.
  • We can use abortBroadcast() method to stop the broadcast for going to next receiver.
Sticky Broadcasts
  • Send broadcast using SendStickyBroadcast()
  • Broadcast will stick with android for future registerReceiver requests.

Q.What are intent filters in android?

  • An intent filter is used to specify the type of intent that a component can receive.
  • By declaring intent filter in our activity with a certain action ,we can make it possible for other application to directly start that activity for using the defined action.
  • Activity can be started using explicit intents only if there no intent filter declared for activity.

Note – You can test your android knowledge in Codesfor Mobile application. You can find all our tutorials,interview questions and answers in the mobile application As well as there is a free test taking platfrom where you can test your android knowledge.

Q.How to identify memmory leaks in android and how to avoid it?

To identify memmory leaks you can use leakcanary library.

  • Use application context instead of activity context wherever possible. In case if it is necessary to pass activity context to any class ,make sure that it is set to null when activity is destroyed 
  • Do not use static variables to declare views or activity context.
  • If you are registering broadcast receiver inside activity make sure you unregister it on onStop method.

More info regarding this topic is available at this link