Monday, January 28, 2013

Activity - Android

What is Activity?
It is basic UI with which user can interact
It creates a window for developer,in which we can place our UI controls.

Activity Life Cycle?
In Android, Activities are managed as stack trace.
[Activity is launched]
|
onCreate()
|
onStart()
|
onResume()
|
[Activity is now visible and user can interact] 
|
If any other Activity tries to come on scree
|
onPause()
|
onStop()
|
onDestroy() 

How to start an Activity?
Android provides us functions startActivity() and startActivityForResult()
Example:
Intent intent = new Intent(runningActivity.this, targetActivity.class);
startActivity(intent);

What is the difference between startActivity and startActivityForResult?
Both the methods starts an Activity.
Activity started via startActivityForResult  would return a result to the calling Activity.


What is intent?
Intent is an abstract description of Action to be performed?
or It is an intention to do an Action.
2 type of information is stored in Intent
(a) Action - The general action to be performed.
(b) Data - Data to operate on

Oh!!! My Activity is killed by OS b'coz there was low memory scenario, How to save data so, that it is not lost?
The Android OS can kill the Activities which are in background i.e. user is not interacting with them, so, whenever our Activity is going in the background we need to save the data, which user would expect on screen when he would come back.

For saving the basic data-type, we can use onSaveInstanceState()
For saving the complex data, we can use onSaveNonConfigData()
Example:
onSaveInstanceState(Bundle data) {
 super.onSaveInstanceState(data);
 data.putlong("key",value); //put data here
}


onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 if(savedInstanceState!=null)
 long l = savedInstanceState.getlong("key"); //Retreive data
}

Monday, December 3, 2012

Similarities and Differences [Interface and Abstract Class]

Interface and Abstract Class

Similarities:
The only similarity between Interface and Abstract class is that both can not be instantiated.

Differences:
  1. An abstract class can have methods with implementation, to provide some default behavior.An interface can not have any implementation.
  2. All the variables in the interface are final by default. An abstract class can have non-final methods.
  3. All the methods in an interface are public by default. An abstract class can have protected and ...
  4. If we want to implement an interface we would use implements. For an abstract class we would use extends.
Whats the need of interface when we have Abstract class?

Why there is a need of Abstract class in Java

Many developers think that Abstract class is a class which can not be instantiated, but can be extended into subclasses. that is whose object can not be created, but there is more to it

Why we need an abstract class?
Abstract class does not have any real time object, but it is required to provide some common characteristics
Example Vehicle - Car, truck,
Car - Maruti, Hyundai, tata Nano

Vehicle is the abstract class, it has tires and steering, car has 4 tyres , truck has 6 or 8 tyres
Car is abstract class, it has tires, steering, dashboard, headlight etc.