Thursday, November 3, 2011

Android Interview Questions - Activity


1. What is an Activity?
    ans:
        An Activity is an application component that provides a screen with which users can interact in order to do something

2. Activity Life Cycle and explain their states?
     ans:
      onCreate()      : Called when the activity is first created.This is where you should do all of your normal static set up — create views, bind data to lists, and so on.
      onStart()        : Called just before the activity becomes visible to the user.Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.
      onResume()   : Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack.Always followed by onPause().
      onPause()      : Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes.
      onStop()        :  Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity has been resumed and is covering it.
      onDestroy()   : Called before the activity is destroyed. This is the final call that the activity will receive.

3. What happen in onCreate()?
    ans:
      Called when the activity is first created.This is where you should do all of your normal static set up — create views, bind data to lists, and so on.

4. What happen in onCreate() and onStart()?
    ans:
       onStart() only the activity comes to visible.onCreate() it does the work of setting up  — create views, bind data to lists, and so on.

5. What happen in onStop() and onDestroy()?
     ans:
       onStop() is called before onDestroy() when there is  no visibility to the user its exists in the task.When onDestroy() is called it make sure the activity is destroyed.It is called when the BACK() button is clicked the activity pops out of the task.

6. In which stage of the activity life cycle do you want to commit the changes?
    ans:
       onPause();

7. Which stage of activity is certain to be called?
    ans:
      onPause();

8. How to stop an activity?
   ans:
      finish();

9. What is Fragments?(Not Important)
    ans:
      A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

10. What is Loaders?(Not Important)
  ans:
     Introduced in Android 3.0, loaders make it easy to asynchronously load data in an activity or fragment.

11. What is  task?
  ans:
     A task is a collection of activities that users interact with when performing a certain job.

12. Can task have other application activity inside it ? Give me an example ?
    ans:
     Yes,from any application we can call email client of android this is also falls in the current task.

13. Can u explain how the activity are loaded in Task?
     ans:
       When the current activity starts another, the new activity is pushed on the top of the stack and takes focus. The previous activity remains in the stack, but is stopped. When an activity stops, the system retains the current state of its user interface.Activities in the stack are never rearranged, only pushed and popped from the stack—pushed onto the stack when started by the current activity and popped off when the user leaves it using the BACK key.    

14. When does the state of the activity is saved?
      onStop();

15. Is all the activity state saved in application stack? If NO what should it does to recover the state?(Important)
     No, the system might destroy that activity completely if it needs to recover system memory. the system still knows that the activity has a place in the back stack, but when the activity is brought to the top of the stack the system must recreate it.

16. What should you do to retain the activity state?(Not Important)
   onSaveInstanceState();

17. How to start a task?
<activity ...="">
    <intent-filter ...="">
        <action android:name="android.intent.action.MAIN">
        <category android:name="android.intent.category.LAUNCHER">
    </category></action></intent-filter>
    ...
</activity>


18. Let there be three activity flow  A-&gt;B-&gt;C  I want to go to A how will i go?
     Please do a sample program for task. and put the comments


Hi guys i have summed up few Activity related interview question's and their answers.If any thing wrong please correct me and add your question's too?Lets all get benefited.

1 comment: