onCreate ()
You must declare to initialize all your variables, you must also initialize the listener
for the buttons and in general all the other components.
onStart ()
is the method that is called when the activity starts to be visible to the user
onResume ()
is the method that is called after onStart()
but it is also the method called when an activity is again accessible to the user, but it is still not visible to him (he thinks that all this happens in milli-seconds), after it has been hidden by the system (for example the user receives a call or leaves the phone on the table and goes to sleep mode). Here it depends on the application itself, for example you can call an API-REST again to reload the data.
onPause ()
is the method called by the OS when an activity is going to background, ie it starts to be out of view of the user (example the phone goes to sleep mode), here you can save data, for example, if you are in an app that has a map you can save the zoom that the user used, latitude and longitude.
In general, everything depends on the application itself, but you must consider what to do in your application when your view goes to the background and what to do when you return from that state, for which we have all these methods, for Solve questions like:
What do I have to do when my app goes to idle mode?
What do I have to do when my app comes out of sleep mode?
What do I have to do if someone is using my app and they call
phone?
Here is an example that I hope will help you understand it
public class TestActivity extends Activity {
/** Called when the activity is first created. */
private final static String TAG = "TestActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(TAG, "On Create .....");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "On Destroy .....");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "On Pause .....");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "On Restart .....");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "On Resume .....");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "On Start .....");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "On Stop .....");
}
}
And here is the life cycle of an activity