Saturday, 3 December 2011

android interview questions -- Content Providers

Whats is content provider?
Content provider is a mechanism to share data between application. Android doesn't provide common area to store data , so this is the only mechanism to share data between applications.

Brief  about how data sharing mechanism works using content provider?
  • Content provider should expose its URI to the applications.
  • Application will send URI to the ContentResolver
  • ContentResolver will send this published query to ContentProvider
  • ContentProvider will send back requested data to ContentResolver(CR), In responce CR send this data to application.
How to query a content provider?
Android provides below methods to make a query
  • ContentResolver.query()
  • Activity.managedQuery()
What query methods return?
Cursor object.

What are the steps to implment our own ContentProvider?
  • Plan DB, URI's ,Column Names ,Column Type etc.
  • Populate DB
  • Extend ContentProvider abstract class
  • Implenet methods query(), insert(), delete(), getType(), update()
  • Register CP through android manifest file.

Thursday, 1 December 2011

Android interview questions - Services

What is Service in android?
Ans: A Service is a android application component which will be used to run long running task.Service will run in back ground. A Service can be created by extending "Service" class.
Unlike thread a Service will run in same process.Service can be started from other android components.

What are the different types of Services?
Ans: Android provide two types of Services:
  1. Local Services
  2. Remote Services
What are the Important/Basic methods in Services?
  • onCreate()
  • onBind()  -- Required if it is remote service
  • onStartCommand()  
  • onDestroy()
What is the significance of Sticky Intents in Services?
Services are low priority components in android and android system can kill services whenever it requires to allocate resources to higher priority tasks then by depending on Stick Intent value it will decide how to restart service.

Can I make my Service run all the time?
Ans: Theoretically it is supposed to be run all the time but generally Android will kill service whenever it need resources such as memory and processor , by returning START_STICKY intent from onStartCommand() we can ask Android system to restart it whenever resources available , another procedure is use AlarmManager to start service periodically.

What is foreground Service?
Ans:Unless background service it will have more priority , by using setForeGround() method we can make it foreground service.

Can I display or update UI from Service?
Ans: NO. To draw any UI component it should be done from UI thread. We can use Notification Manager to send notifications to the user.

If I start same service multiple times does it create multiple instances of Service?
Ans: NO.






Android interview questions - Activities

What is activity?
Ans: The user interface of an application is displayed on a device through an Activity, Mostly with one Activity created for each unique screen.
An activity can be created by extending "Activity" class from "android.app.*" package.
Activity class take care of creating a window for your application.

What are the important methods in Activity class?
Ans:   onCreate()
          onStart()

          onResume()
          onPause()
          onStop()
         onDestroy()
Note: Flow order of above methods will depend on activity state.

If activity 'A' fully obscured by activity 'B' then which methods will be called in activity 'A'?
Ans: onPause()  --> onStop()

If activity 'A' partially covered by another screen then which methods will be called in activity 'A'?
Ans:  onPause()

If we display dialog box from current activity then what would be the state of current acivity?
Ans: Activities state will remain same , because Alert dialog box will be considered as a child view.

When activity is in foreground and user presses 'Home' button then which methods will be called?
Ans: onPause() --> onStop(). Home screen is also an activity.

What is the life cycle of Activity in android?
Ans: Below diagram depicts the life cycle of android.

How to kill/destroy activity ?
Ans: By calling activities finish() method.


When onDestroy() will be called in Acivity?
Ans: Typically for 2 reasons
       1.call to finish() from application
       2.Android run time decides to kill your activity.

Is it required to declare every activity in Android manifest file?
Ans: Yes. If not declared then that activity will not visible to Android system and it will never run.

What is ListActivity in android?
Ans:This is an activity that displays the list of items by binding to the data source. This activity provides handy methods to manage list of items.

What is difference between Activity and ListActivity?
Ans:ListActivity provides the easy to use methods to manage list of items.(ListView)