Adsense Alternatives Technology Blog Directory Blog Listings Activity In Android | Target Studios

Friday, August 21, 2015

Activity In Android

Activity In Android



An Activity is a pre-defined class in Android and every application which has User Interface must inherit it to create window.If you have worked with C/C++ or Java language then you must have seen that your program starts from main() function.


In C/C++ :  void main() { }
In Java : public static void main(String args[]) { }


In a very similar way, Android system initiates its program with in an Activity starting with a call on onCreate(). Almost all activities interact with the user, so the Activity class creates a window for you in which you can place your text, images,etc. with setContentView(View) method.



If you see our previous article, " First Android App ", you will notice that in MainActivity.java, the Main Activity is initiated with a call to :

" protected void onCreate(Bundle savedInstanceState) " method.

savedInstanceState means to check the last activity performed by user. eg. pressing a button.


In this method, you will find  " super.onCreate(savedInstanceState) " and " setCotentView(R.layout.activity_main) "


Thus, the setContentView set the User Interface to activity_main.xml



Life Cycle Of An Activity




Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.


An activity has essentially four states:
1) If an activity in the foreground of the screen (at the top of the stack), it is active or running.

2) If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.

3) If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.

4) If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.


The following diagram shows the important state paths of an Activity. The square rectangles represent callback methods you can implement to perform operations when the Activity moves between states. The colored ovals are major states the Activity can be in.








Starting Activities


The startActivity(Intent) method is used to start a new activity. It takes a single argument, an Intent, which describes the activity to be executed. If you check your AndroidManifest.xml file,
under
This is used to start an activity.
eg.


startActivity( new Intent("android.intent.action.MAIN"));

The above snippet will start a new activity ie. Main.


An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity or broadcastIntent to send it to any interested BroadcastReceiver components.





Thank You

No comments:

Post a Comment