Activity Life Cycle Example in Android
Activity Life Cycle Example in Android
More
Next Blog
Create Blog
Sign In
android examples
WEDNESDAY, JULY 27, 2011 FOLLOWERS Join this site
w ith Google Friend Connect
Members (9)
BLOG ARCHIVE
2011 (6) July (1) Activity Life Cycle Example In Android June (5)
ABOUT ME
The above figure shows how and when activity is created and when it is paused and destroyed. <<kill>> means when the resources are low OS will kill the activity. onCreate() ::This method is called when first time activity is created. onStart()::This method is called just before your activity becomes visible on the screen. onResume()::This method is called after onStart() method and if your activity is the foreground activity on the screen. onPause()::This method is called when your activity is just about to call another activity so that the current activity has to be paused and the new activity has to be resumed. Here the previous activity is not stopped but it loss the foreground
visibility means it goes as background activity. onStop()::This method is called when your activity is no longer visible on the screen. onDestroy()::This method is called when your current activity has the last chance to do any processing before it is destroyed. Activity Life Cycle Example :: Create a new project with the name BasicActivityExample and create a class with the name LifeCycleActivity as a main activity.
// L i f e C y c l e A c t i v i t y . j a v a
p a c k a g ec o m . a n d r o i d . t u t o r i a l ; i m p o r ta n d r o i d . a p p . A c t i v i t y ; i m p o r ta n d r o i d . o s . B u n d l e ; i m p o r ta n d r o i d . w i d g e t . T o a s t ; p u b l i cc l a s sL i f e C y c l e A c t i v i t ye x t e n d sA c t i v i t y{ / * *C a l l e dw h e nt h ea c t i v i t yi sf i r s tc r e a t e d .* / @ O v e r r i d e p u b l i cv o i do n C r e a t e ( B u n d l es a v e d I n s t a n c e S t a t e ){ s u p e r . o n C r e a t e ( s a v e d I n s t a n c e S t a t e ) ; s e t C o n t e n t V i e w ( R . l a y o u t . m a i n ) ; T o a s t . m a k e T e x t ( t h i s ," o n C r e a t e ( ) " ,
T o a s t . L E N G T H _ L O N G ) . s h o w ( ) ;
} @ O v e r r i d e p r o t e c t e dv o i do n S t a r t ( ){
/ / t h ea c t i v i t yi sb e c o m ev i s i b l e . s u p e r . o n S t a r t ( ) ;
T o a s t . m a k e T e x t ( t h i s ," o n S t a r t ( ) " ,
T o a s t . L E N G T H _ L O N G ) . s h o w ( ) ;
} @ O v e r r i d e p r o t e c t e dv o i do n P a u s e ( ){
s u p e r . o n P a u s e ( ) ;
T o a s t . m a k e T e x t ( t h i s ," o n P a u s e ( ) " ,
T o a s t . L E N G T H _ L O N G ) . s h o w ( ) ;
} @ O v e r r i d e p r o t e c t e dv o i do n R e s u m e ( ){
/ /T O D OA u t o g e n e r a t e dm e t h o ds t u b s u p e r . o n R e s u m e ( ) ;
T o a s t . m a k e T e x t ( t h i s ," o n R e s u m e ( ) " ,
T o a s t . L E N G T H _ L O N G ) . s h o w ( ) ;
} @ O v e r r i d e p r o t e c t e dv o i do n S t o p ( ){
/ / t h ea c t i v i t yi sn ol o n g e rv i s i b l e . s u p e r . o n S t o p ( ) ;
T o a s t . m a k e T e x t ( t h i s ," o n S t o p ( ) " ,T o a s t . L E N G T H _ L O N G ) . s h o w ( ) ; } @ O v e r r i d e p r o t e c t e dv o i do n D e s t r o y ( ){
/ / T h ea c t i v i t ya b o u tt ob ed e s t r o y e d . s u p e r . o n D e s t r o y ( ) ;
T o a s t . m a k e T e x t ( t h i s ," o n D e s t r o y ( ) " ,
T o a s t . L E N G T H _ L O N G ) . s h o w ( ) ;
} }
In the above program we wrote all the Life Cycle Methods. Here Toast.makeText(...) method is used to display the text on the screen. //main.xml
< ? x m lv e r s i o n = " 1 . 0 "e n c o d i n g = " u t f 8 " ? > < L i n e a r L a y o u t x m l n s : a n d r o i d = " h t t p : / / s c h e m a s . a n d r o i d . c o m / a p k / r e s / a n d r o i d " a n d r o i d : o r i e n t a t i o n = " v e r t i c a l " a n d r o i d : l a y o u t _ w i d t h = " f i l l _ p a r e n t " a n d r o i d : l a y o u t _ h e i g h t = " f i l l _ p a r e n t " > < T e x t V i e w a n d r o i d : l a y o u t _ w i d t h = " f i l l _ p a r e n t " a n d r o i d : l a y o u t _ h e i g h t = " w r a p _ c o n t e n t " a n d r o i d : t e x t = " W a i t1 0s e c o n d sa n ds e et h es c r e e ns h o w sh o wm e t h o d s a r ee x e c u t e d . \ n A f t e r1 0s e c o n d sc l i c ko nE S Co nt h ek e y b o a r ds e ew h a tm e t h o d sa r e e x e c u t e d " / > < / L i n e a r L a y o u t >
The above program is the best and simple example how the activity methods will execute one by one.Remember when the application is started see the bottom it
display's which methods are exeuted when an an activity is running. After all the few seconds click on ESC in the keyboard or press back button in your emulator then you may see the methods which are executed when an application is destroyed.
PLEASE LET ME KNOW IF U ARE FACING ANY PROBLEMS WITH THIS APPLICATION AND U NEED TO KNOW MORE INFORMATION REGARDING THIS ACTIVITY LIFE CYCLE PLEASE COMMENT IT.
3 comments:
Venkateswara Reddy July 15, 2012 at 4:06 AM hi,this information is good ,for a beginner but it should be in more clarity. Reply
Venkata Reddy Nallamilli November 1, 2012 at 11:37 PM ok good.but try to give the example with more Activities. by using that we can understand more about the lifecycle phases... Reply
Thulasiram December 11, 2012 at 2:02 AM This comment has been removed by the author. Reply
E n t e ry o u rc o m m e n t . . .
Home
Subscribe to: Post Comments (Atom)
Older Post