0

I have a question related to the call back methods of Activity Life cycle. Android has provided different methods oncreate() , onStart() etc. My Question is when we have to use which method in our application ? Means to say that Which method should use for different scenario ? what type of code one should do in all the methods according to the requirement? Hope You understand my question.

Deepak Sharma
  • 4,999
  • 5
  • 51
  • 61

2 Answers2

4

You can refer this.

onCreate: This method is called only once. Use it for initialization of all your views such as TextView, Layouts, etc.

onResume: It will be called every time your application will come to foreground from background. So if you have any broadcast receiver, which you want to be working only when your application is in foreground, you can probably register it here.

onPause: It will be called just before your application moves to background (may be when you receive any call or click any notification of other app in notification bar.) So if you want to do some task when your app is moving in background, this is the place you can do it. Again the best example would be of unregistered any broadcast receiver. (Broadcast receiver is just one example. Not the only use of onResume and onPause)

onDestroy: It will be called before your app is going to exit. So if you want to do something before app exit, you can do here. May be send a error report in case of crash. Or stop a service which was being executed.

Hope this gives a hint.

PedroHawk
  • 622
  • 5
  • 19
2

Its important you learn all about this article:

-Android activity lifecycle

-what/when/how implement activity's native methods

http://developer.android.com/reference/android/app/Activity.html

hope it helps

PedroHawk
  • 622
  • 5
  • 19