Friday, March 18, 2011

Activity In Android

1) What are the 4 basic Component in Android ?

  • Activities 
  • Services 
  • Broadcast receivers 
  • Content providers 
2) What is an Activity ?
Answer :- An activity is the equivalent of a Frame/Window in GUI toolkits. It takes up the entire drawable area of the screen (minus the status and title bars on top). An activity is what gets bound to the AndroidManifest.xml as the main entry point into an application

3) What is the lifecycle of an Activity ?




4) If any Activity is running, in which state it goes when we press "Back" button and "Home" button ?
Answer :-

Back -> onPause -> onStop -> onDestroy . If again i start the Activity then onStart -> onResume -> Activity is visible.
Home -> onPause -> onStop -> If again i start the Activity then onRestart -> onStart -> onResume -> Activity is visible.

5) What are the 2 ways of starting Activity ?
Answer :-
Two ways to start Activity is
(i) public abstract void startActivity (Intent intent)
            Context.startActivity(intentObject);
(ii) public void startActivityForResult (Intent intent, int requestCode)

When we call the 2nd method ?
One activity often starts the next one. If it expects a result back from the activity it's starting, it calls startActivityForResult() instead of startActivity(). For example, if it starts an activity that lets the user pick a photo, it might expect to be returned the chosen photo. The result is returned in an Intent object that's passed to the calling activity's onActivityResult() method.

6) How you calculate the height and width of canvas in Activity class?
Answer :-
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();

7) How we pass information between one activity to another activity? How we pass object and ArrayList etc.
Answer :-
Let assume that there is a two Activity i.e A and B. we have to pass a data from Activity A to Activity B.

public class A extends Activity
{
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
         // CODE HERE
         Intent i = new Intent( this, B.class );
         i.putExtra( "int", 5);
         i.putExtra( "string", "hello" );
         public Intent putIntegerArrayListExtra (String name,
         ArrayList<Integer> value);
         public Intent putStringArrayListExtra (String name,
         ArrayList<String> value);
         public Intent putExtra (String name, Serializable value);
         public Intent putExtra (String name, Bundle value);
         startActivity( i );
   }
}
public class A extends Activity
{
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
         // CODE HERE
         Intent i = getIntent();
         int number = i.getIntExtra("int", -1);
         String str = i.getStringExtra("string");
         public ArrayList<Integer> getIntegerArrayListExtra (String name);
         public ArrayList<String> getStringArrayListExtra (String name);
         public Serializable getSerializableExtra (String name);
         public Bundle getBundleExtra (String name);
   }
}

8) How we can open a browser from a Activity/View ?
Answer :-
            Intent launchBrowser = new Intent();
            Uri url=Uri.parse("http://www.ea.com");
            launchBrowser.setAction(Intent.ACTION_VIEW);
            launchBrowser.setData(url);
            context.startActivity(launchBrowser);

9) When onSaveInstanceState() is call?

10) Suppose there is 2 Activity i.e Activity A and Activity B? A is in foreground and B in on background? What is the state of both activity?






































1 comment: