Sunday, 13 August 2017

Android Activity Life Cycle Tutorial with example


Android Activity Life Cycle

Importance of Activity's Life cycle

Understanding Android Activity Life cycle is one of the most important things for android developers. Using the activity life cycle correctly can avoid your application from:
  • Losing the user data when the screen orientation changes
  • Crashing of application while switching between applications
  • Consuming valuable resources of the phone while the user is not using you app
  • Losing the users data when user leaves you app and return later
Here an important point is, the activity is recreated with the phone's orientation is changes. So to give your users best experience, you need to take care of this too.

Activity Life cycle Callbacks

To give the users of your application best user experience, it is very important to understand the activity life cycle. To manage you activities, android provides various callbacks on different stages of activity life cycle. They are listed below:
  1. onCreate()
  2. onStart()
  3. onResume()
  4. onPause()
  5. onStop()
  6. onRestart()
  7. onDestroy()
They all are called on different stages of activity life. To understand which callback is called when, see the table and image from the official documentation of android below carefully. And also see the demo project running given at the end of the tutorial.
Android Activity Life Cycle


MethodDescription
onCreatecalled when activity is first created. You must implement this callback.
onStartWhen the activity enters the Started state, the system invokes this callback. The system prepares to enter the activity to foreground.
onResumecalled when activity will be in visible form to the user. This is the state in which the app interacts with the user.
onPausecalled when activity is not in focus. It is the first indication that the user is leaving the activity. It does not mean that activity is going to destroy. When a new dialog(popup) or multi window in android 7.0 and higher is made, it is also called as the app loses the focus.  
onStopcalled when activity is no longer visible to the user. It usually happens when a new activity is launched.
onRestartcalled after your activity is stopped, prior to start.
onDestroycalled before the activity is destroyed. After that your activity will be removed from the memory.

Save the State of your App

To save the state of your application, android provides two callbacks onSaveInstanceState(Bundle savedInstanceState) and  onRestoreInstanceState(Bundle savedInstanceState). From these two callbacks, you can save and restore the data and make the user experience better. These callbacks are called when the orientation of the app is changed or app is brought to foreground again. onSaveInstanceState(Bundle savedInstanceState) is called after onPause() and similarly onRestoreInstanceState(Bundle savedInstanceState) is called onStart() method.

Demo Project

To understand the activity life cycle more clearly, run the demo project at  Github Repository. Here you will see all the callbacks in action and also learn how you can save the user data and make the better user experience. If you want to know anything about it or having some confusing, feel free to comment or contact me at farooqahmadkhan003@gmail.com


48 comments:

  1. Your post is really very helpful. Easy to learn as you explain things precisely. Thank you so much.

    android courses in chennai

    android mobile apps development training in chennai


    ReplyDelete
  2. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.

    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    iot-training-in-chennai

    ReplyDelete
  3. Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!
    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  4. Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me 
    python training in OMR
    python training in tambaram
    python training in annanagar

    ReplyDelete
  5. Good Post, I am a big believer in posting comments on sites to let the blog writers know that they ve added something advantageous to the world wide web.
    Blueprism training in Pune

    Blueprism training in Chennai

    ReplyDelete
  6. 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

    ReplyDelete
  7. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  8. Thanks for sharing the useful blog about Android Activity Life Cycle Tutorial with Example.

    Android Application Development Company

    ReplyDelete
  9. i Digital Academy ensures that each of you gain maximum knowledge based on current web trends, hence we employ trainers who are working in MNC. Making you ready for job and assisting you to get a job in Angularjs is our priority. Our Angularjs trainer focus on each and every detail to make sure you gain the best knowledge in terms of Angularjs . Your trainers with extensive experience in Angularjs and connections will refer you to companies that are looking for fresh talent after successful web development course completion.

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

    ReplyDelete
  11. Thank you so much for sharing information.
    This post helped me in Understanding Lifecycle in Android Activiy
    .keep up the good work by sharing such blogs in future.This blog was really helpful.

    ReplyDelete
  12. Set aside my effort to peruse all the remarks, however I truly delighted in the article. It's consistently pleasant when you can not exclusively be educated, yet in addition, engaged!
    360DigiTMG Data Analytics Course

    ReplyDelete
  13. Hello there to everyone, here everybody is sharing such information, so it's fussy to see this webpage, and I used to visit this blog day by day
    what is the difference between analysis and analytics

    ReplyDelete
  14. The information you have posted is important. The objections you have insinuated was worthy. Thankful for sharing.
    data scientist malaysia

    ReplyDelete
  15. I like this post,And I figure that they making some incredible memories to scrutinize this post,they may take a good site to make an information,thanks for sharing it to me
    iot course in noida

    ReplyDelete
  16. The blog and information is incredible and useful also
    PMP Certification

    ReplyDelete
  17. Always so interesting to visit your site.What a great info, thank you for sharing. this will help me so much in my learning
    business analytics course

    ReplyDelete