Wednesday, July 27, 2011

Activity Life Cycle Example In Android

Activity Life Cycle ::

Below shows the methods of Activity
a)onCreate()
b)onStart()
c)onPause()
d)onResume()
e)onStop()
f) onDestroy()


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.


// LifeCycleActivity.java
package com.android.tutorial;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class LifeCycleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(this, "onCreate()", Toast.LENGTH_LONG).show();
}
@Override
protected void onStart() {
//the activity is become visible.
super.onStart();
Toast.makeText(this, "onStart()", Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this, "onPause()", Toast.LENGTH_LONG).show();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume()", Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
//the activity is no longer visible.
super.onStop();
Toast.makeText(this, "onStop()", Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
//The activity about to be destroyed.
super.onDestroy();
Toast.makeText(this, "onDestroy()", Toast.LENGTH_LONG).show();
}
}

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Wait 10 seconds and see the screen shows how methods are executed.\n
After 10 seconds click on ESC on the keyboard see what methods are executed"
/>
</LinearLayout>

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.



65 comments:

  1. hi,this information is good ,for a beginner but it should be in more clarity.

    ReplyDelete
  2. ok good.but try to give the example with more Activities.
    by using that we can understand more about the lifecycle phases...

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Thank you. Good and simple one for beginner.

    ReplyDelete
  5. Really wel said about activity life cycle in android.. Android Training in chennai

    ReplyDelete
  6. Nice information about android activity life cycle and the example that was share is very much useful and helpful My sincere thanks for sharing this post
    Android Training in Chennai

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries.
    Click here:
    angularjs training in sholinganallur
    Click here:
    angularjs training in btm

    ReplyDelete
  9. Really great post, Thank you for sharing This knowledge.Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place. Please keep it up!
    Click here:
    Microsoft azure training in velarchery
    Click here:
    Microsoft azure training in sollinganallur

    ReplyDelete
  10. This is such a great post, and was thinking much the same myself. Another great update.
    Click here:
    python training in tambaram
    Click here:
    python training in annanagar

    ReplyDelete
  11. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    Blueprism training in Chennai

    Blueprism training in Bangalore

    Blueprism training in Pune

    Blueprism online training

    ReplyDelete
  12. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    Devops Training in pune

    Devops Training in Chennai

    Devops Training in Bangalore

    ReplyDelete
  13. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    java training in chennai | java training in USA

    ReplyDelete
  14. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    java training in marathahalli | java training in btm layout

    java training in jayanagar | java training in electronic city

    ReplyDelete
  15. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    AWS Interview Questions And Answers


    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    AWS Training in Pune | Best Amazon Web Services Training in Pune

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course

    ReplyDelete
  16. We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!
    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    angularjs Training in bangalore

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    ReplyDelete
  17. I think things like this are really interesting. I absolutely love to find unique places like this. It really looks super creepy though!! machine learning training in chennai

    artificial intelligence and machine learning course in chennai

    machine learning classroom training in chennai

    ReplyDelete
  18. This is really impressive post, I am inspired with your post, do post more blogs like this, I am waiting for your blogs.
    Regards,
    Devops Training in Chennai | Devops Certification in Chennai

    ReplyDelete
  19. our sequence is very unique and vague. I can observe few more things which I left in my blog.
    Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete

  20. Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts

    angularjs online training

    apache spark online training

    informatica mdm online training

    devops online training

    aws online training

    ReplyDelete
  21. Hey Nice Blog!! Thanks For Sharing!!! Wonderful blog & good post. It is really very helpful to me, waiting for a more new post. Keep Blogging ! Here is the best
    angularjs training online with free Bundle videos .

    contact No :- 9885022027.
    SVR Technologies

    ReplyDelete
  22. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing.. windows azure training

    ReplyDelete
  23. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.thanks

    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  24. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    DevOps Training in Chennai

    DevOps Course in Chennai


    ReplyDelete
  25. Kim Ravida is a lifestyle and business coach who helps women in business take powerful money actions and make solid, productiveIamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder IamLinkfeeder

    ReplyDelete
  26. We are used to the fact that we know only religious and public holidays and celebrate only them.buyseoservice2 buyseoservice2 buyseoservice2

    ReplyDelete
  27. Theodore Hall (Ted) is a retired manufacturing executive who has gotten into Internet Marketing कल्याण मटका रिजल्ट Kalyan matka Satta Result

    ReplyDelete
  28. K.G.F: Chapter 2 : Directed by Prashanth Neel. With Yash, Sanjay Dutt, Raveena Tandon, Prakash Raj. The blood-soaked land of Kolar Gold Fields

    ReplyDelete
  29. Tired of sharing long, nasty URLs? This app immediately shortens URLsCRYPTO NEWS 24X7

    ReplyDelete
  30. Organization easy two. Camera lose his ability method middle part pick.insightminutes.com

    ReplyDelete
  31. Offer here tough test. Late raise deep woman man collecti

    ReplyDelete