Hello Friends here are some Interview Question I prepare during my interview.
1) Activity life cycle- Can I save all my database updates in onStop()?
Ans.: No because onStop() may not be called in some situations.
In case of low memory or configuration changes, there is a chance that android may force-kill your applications befor 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.
We can't save all database table in onSaveInstanceState because that function will not be called if user presses back button.
2) What is the difference between 'this' and 'getapplicationcontext'?
Ans. : 'this' points to current context. 'getapplicationcontext' point to entire process. If your context is of entire life time of process then use 'app context' else 'this'.
If you want to use some control whose life time is through out your application life time then go for 'app context', else use 'this' pointer(activity context).
3) What is the UI response time limit in android?
Ans. 5sec
If user touches your activity and if your program doesn't respond to user events within 5 seconds, android will not tolerate it and it force closes your application this error is called as ANR(Application Not Responding).
4) What is ANR(Application Not Responding)? What is the reason for this problem and what is the solution for that problem?
Ans. ANR- will occur if we are doing any other heavy functionality along with UI in single Main Thread. If two heavy functionalities happen in single thread. it will delay response to user actions, which may irritate user, and hence stop your process.
Solution- Run only UI components in Main Thread.
5) What is Bundle? What dose it contain in onCreate() of your activity?
Ans. Bundle contain previous savedinstancestate.
Bundle is a data holder, which holds data to be passed between activites. 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 store its states once android recreates a killed activity.
This will happen in case of low memory or configuration changes like rotating the phone.
onSaveInstanceState():
This function will be called by a Android before onPause() or after onPause(). If Android is forcefully killing your acclivity. In this function we have to save all your activity states.
onRestoreInstanceState():
This function will be called after onStart()
6) How serializable differ from parcel?
Ans. Parcel is not a general-purpose serialization mechanism. This class is designed as a high-performance IPC transport. used heavitly in Binders. for normal serialization concepts, use serializable . since serializable are heavy and takes time. we use Parcels for IPC in embeded devices.
7) How to analyze android application crash, how to fix crash using logcat?
Ans. after crash logcat will contain file name, exception name along with line number where it has crashed.
8) What is a singleton Class?
Ans. A class for which we can create only one object is called as singleton class.
9) How to create a singleton class in Java?
Ans. Singleton class means a class for which only one object exists. It is impossible to create multiple objects for a singleton class.It is a design pattern.
To create a singleton class:
Don't expose public constructors from your class. Because if constructor is public, then any one can create object for your class. Then use public static method to create an object for your class and return it. If clients want object for your class then they can call this public static method directly with class name.
Note: Make sure that you are creating object only once in this static method.
eg.
public class MySingleTon{
private static MySingleTon myObj;
/**
* Create private constructor
*/
private MySingleTon(){
}
/**
* Create a static method to get instance.
*/
public static MySingleTon getInstance(){
if(myObj==null){
myObj=new MySingleTon();
}
return myObj;
}
public void getSomeThing(){
//do something here
System.out.println("I am here...");
}
public static void main(String args[])
{
MySingleTon st=MySingleTon.getInstance();
st.getSomeThing();
}
}
10) What is sleep mode in android? What will happen to CPU once screen(LCD) light goes off?
Ans. Sleep mode-means CPU will be sleeping and will not accept any command except from RIL(Radio Interface Layer) and alarms. CPU will go to sleep mode with in fracton of seconds after LCD is turned off.
There is small difference in power consumption when you compare a phone with laptops.
Laptops most of the time runs on direct power through charger(Mostly we carry charge with laptop).
Mobiles mostly run on battery power(Rarely we carry charger with us) by kepping this in mind. android has designed in a such a way that phone will not consume battery power if user is not interacting with the phone. This is to increase battery back up time for user.
Thats why when LCD screen power is off(that means user is not interacting with phone) within fraction of seconds CPU will also go to a mode(sleep mode) where it does minimal work and saves battery power. When CPU is in sleep mode. it accepts commands only from RIL(radio interface layer) which is basically sms and call functions and alarms. Other than this CPU will stop executing other applications functions.
Note: If any application wants CPU time for its emergency work when CPU is in sleep mode. then it can request CPU time by using WAKE LOCKS.
For eg. MP3 application has to keep playing songs in its service, even though user has turned off LCD screen. That means MP3 application's service has requested CPU time by using WAKE LOCKS.
11) What is a fragment in android?
Ans. Fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity.
Fragment are used to efficiently use the space in wider screen devices.
An activity may contain zero or multiple number of fragments based on the screen size. A fragment can be reused in multiple activities, so it acts like a reusable component in activities.
12) What is the difference between activity and fragment in android?
Ans. Fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity. Where as the complete screen with which user interface is called as activity. An activity can contain multiple fragments. Fragments are mostly a sub part of an activity.
An activity may contain zero or multiple number of fragments based on the screen size. A fragment can be reused in multiple activity so it acts like a reusable component is activities.
A fragment cannot exist independently. It should be always part of an activity. Where as activity can exist without any fragment in it.
13) What is the use of 9patch image compared to other images?
Ans. It will scale the images automatically based on the device screen sizes on which app is loaded.
14) |What is the maximum memory limit given for each process or application in android?
Ans. 16MB is the maximum memory limit given to any given android application. Some second generation phone will return 24Mb of memory for each process or even more.
15) Which kernel(operating system) is used in android?
Ans. linux
Linux modified kernel(Monolithic) is used in Android.
16) How to get contact phone number from contacts application's content provider?
Ans. Use ContactsContents URI and CommonDataKinds Phone CONTENT_URI. then request it through content resolver
//First get the all basic details from basic table of contacts
Cursor c1=this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
c1.moveToNext();//move to first row
String id=c1.getString(c1.getColumnIndex(Contacts_ID));//get its id
//now based on that id, you can retrive phone details from other table
Cursor cur=this.getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI,null,CommonDataKinds.Phone.CONTACT_ID+"=?",new String[]{id},,null);
cur.moveToNext();
String number=cur.getString(cur.getColumnIndex(CommonDataKinds.Phone.NUMBER)):
17) How to upgrade SQLite database of an android application, befor uploading the new version into play store?
Ans. Pass new version number for database to SQLiteOpenHelper() function Then update the database in onUpdate of SQLiteOpenHelper class.
If you want to upgrade the data base of your existing application, which is released into android play store, then follow bellow steps.
- Create new version number for latest database
- Based on some condition pass this latest version number to SQLiteOpenHelper constructor function.
- then what ever the updations you want to make to database, do it in on Upgrade() of SQLiteOpenHelper class.
18) What is the difference between intent, sticky intent, and pending inetent?
Ans. Intent- is a message passing mechanism between components of android except for content provider, sticky intent- Sticks with android, for future broad cast listeners; Pending Intent- Will be used when some one wants to fire an intent in future and may be at that time that app is not alive.
Sticky Intent- Sticks with android, for future broad cast listeners. For example if BATTERY_LOW event occures then that intent will be stick with an android so that if future user requested for BATTER_LOW, it will be fired.
Pending Intent- If you want some one to perform any Intent operation at future point of time on behalf of you, then we will use Pending Intent Eg. Booking a ticket at late night when your application is not running IN this scenario we will create a pending intent to start a service which can book tickets at late night and hand it over to Alarm Manager to fire it at that time.
19) What is the difference between service and a thread?
Ans. Service is a component of android, which runs in the background without any UI. By deault service will run in Main thread only.
Thread is not android component, but still one can use thread to do some background task. Using thread in place of service is discouraged.
Though conceptually both looks similar there are some crucial differentiation.
- Service- If it is destroyed while performing its job, in the middle by Android due to low memory scenario.Then android will make sure that it will restart your service, if you have returned START_STICKY or START_DELIVER_INTENT from onStartCommand().
- Thread- if it is destroyed by android in middle due to low memory, then android will not guarantee to restart it again. That means user lost his half work.
- Service- is a component of android, so it has priority levels to be considered while destroying an application due to low memory. Thread- is not a component of android, so android will not take thread priority into considerations while killing an application due to low memory.
I will to explain this 3rd point
Lets say you have a requirement of connecting to internet from your activity. You can do it by using a service(with thread) or directly by creating a thread in activity. Consider the second scenario where you are connecting to internet in a thread. Then
- What will happen if user closes the activity. while still thread is running in the background. Will that thread continue to run in back ground? Answer is you cannot really predict.
- Assume that in continuation for above scenario, even after killing activity your thread continued to do its intended operation. Then there is a low memory situation aries in your phone. Then this application will be the first susceptible app to be killed as there is no priority for this application.
- so bottom line is If you want to do some heavy background functionality then it is always better to have a service with thread. If you fell that that background functionality to be alive as long as your activity is alive, then go for activity with thread or activity with async task.
20) What is difference between thread and handler thread, in android?
Ans. Handler thread will have looper and MessageQueue, but looper is prepared to handle incoming message.
Handler thread is extended version of thread, where its looper will be prepared to handle incoming messages from other threads(inter thread communication) But for normal java thread, looper will be there but in pasive node, i.e not prepared for inter thread communication.