Saturday, January 3, 2015

Android Interview Questions - part 5

Is it mandatory to implement oncreate() & onstart() of activity life cycle ? Will it run if those life cycle methods are removed?
It is a standard, that programmer has to implement corresponding life cycles methods of activity based on the functionality supported. But one can skip those functions if programmer wish to.


Is it possible to have an Activity without UI?
Generally every activity will have UI (User Interface) screens. But if programmer wants he can omit UI and do some background functionality with an Activity.
Note: To do background functionality its better to use service than an activity with out UI. It makes more sense.


Some important points to remember about activity life cycle:
When a dialog comes on top of an existing activity, then existing activity will move to partially invisible state by calling onPause().
When lcd goes off, activity will be moved to onPause() state. This is a special case.


Android ANR(Application Not Responding) : What is the UI response time limit in android. (I.E with in how many seconds main thread has to respond to user actions?)
If user touches your activity, and if your program doesn't respond to user events with in 5 seconds, android will not tolerate it and it force closes your application. this error is called as ANR (Application Not Responding).


Activity life cycle: What is the mandatory activity life cycle function that will get called in case of configuration changes?
In case of low memory or configuration changes, android will call both onPause() and onSaveInstanceState() with out fail. Exception: there is one exceptional case, that is if programmer is handling configuration changes, then in that case it will not call those functions.
Activity life cycle onStop: Can I save all my databse table updates in onStop() of activity? If not explain why and also explain where should I save db tables?
In case of low memory or configuration changes, there is a chance that android may force-kill your application before reaching onStop(). onPause() is the only function that will be called with out fail before killing the application. so save all persistent data like DB tables in onPause() only.
Note : We can't save all database tables in onSaveInstanceState, because that function will not be called if user presses back button.


Save image in onsaveinstatncestate: A heavy image downloaded from internet in onCreate() of an activity. Is it possible to save it in onsaveinstancestate() in case of configurationChanges?
Generally configuration changes causes activity to get destroyed and recreated. So if you have downloaded heavy image from internet in onCreate() function, then there is a chance that it will be re downloaded in onCreate() one more time if user rotates the phone. To avoid this we can have a static reference to that image to avoid multiple downloads.


What is the difference between px, dp, dip, and dpi ?
px - is meant for absolute pixels. This is used if you want to give in terms      of absolute pixels for width or height. Not recommended.


dp/ dip - both mean same, density independent pixels. This is alternative of      px. Generally we never use px because it is absolute value. If you use px      to set width or height, and if that application is being downloaded into       different screen sized devices, then that view will not stretch as per the     screen original size. dp is highly recommended to use in place of px.
  Use dp if you want to mention width and height to grow & shrink dynamically    based on screen sizes.
  
  if we give dp/dip, android will automatically calculate the pixel size on      the basis of 160 pixel sized screen.


sp - Scale independent pixels. when mentioning the font sizes to fit for          various screen sizes, use sp. This is similar to dp.Use sp especially for      font sizes to grow & shrink dynamically based on screen sizes.


dpi- dots per inch. It is the resolution factor. More the dpi better and    higher the screen resolution.


Math logic behind it:
1dp = 1px in 160 dpi screen. Means 1 dp is equal to 1physical px of a device having 160 dots per inch.
so no of dynamic pixles for a given dp is = (dp's)*(density/160).
For example on 240dpi screen 1dp = 1.5px (physical pixels)


What is android:gravity attribute in the view tag?
android:gravity property is used to align the view content either right/top/bottom/center/.. with in that view.


what is the life cycle of an activity in case of configuration change or orientation change?
Configuration change : is either orientation change(phone rotation), or language settings change.
In case of configuration change, android will forcefully kill your activity and recreates it, unless you are not handling configuration changes on your own programmatically.


When android kills your activity due to configuration changes, it makes sure that it will definitely call onPause() and onSaveInstanceState. But there is no guarantee about call onStop and onDestroy. But android documentation doesn't say that it will not call onStop and onDestroy. it depends.


Life cycle in the case of configuration changes can be any of the option 1, or 2, or 3, based on the situation. some times it might not kill activity at all if programmer is handling configuration changes programmatically, which is not a standard way to do.


Activity life cycle: Why you should not do heavy functionality in onPause()  of your activity?
onPause() will be called as the first indication that user is moving away from your activity, that means intentionally user is moving to next screen, so more the time you hold control in onPause() more it will irritate user. so don't hold control here for too much of time.


What is Bundle? What does it contain in onCreate() of your activity?
Bundle is a data holder, which holds data to be passed between activities.
In case of forceful closing of an activity, android will save all its UI states and transient states so that they can be used to restore the activity's states later. This saved states will be passed via Bundle in onCreate() function to restore its states once android recreates a killed activity.
EG: this will happen in case of low memory or configuration changes like rotating the phone.


onSaveInstanceState():
This function will be called by aAndroidAndroidndroid before “ompause” or after “onpause” if aAndroidAndroidndroid is forcefully killing your activity. In this function we have to save all your activity states.


onRestoreInstanceState():

This function will be called after “onStart”.

Own a smart phone with exciting offers:

2 comments: