The methods related to the life cycle of an Activity are called automatically, even if you do not see them explicitly in their code they exist, this is the basic skeleton of an activity:
public class ExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
}
@Override
protected void onStart() {
super.onStart();
// The activity is about to become visible.
}
@Override
protected void onResume() {
super.onResume();
// The activity has become visible (it is now "resumed").
}
@Override
protected void onPause() {
super.onPause();
// Another activity is taking focus (this activity is about to be "paused").
}
@Override
protected void onStop() {
super.onStop();
// The activity is no longer visible (it is now "stopped")
}
@Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
}
}
Remembering that your code should ALWAYS be after the superclass call implementation.
Depending on the application created it is necessary to edit these methods, it is a good practice, the connection to the database is closed when the application enters onPause()
and reopened in onResume()
to avoid spending resources, this occurs, for example, when someone receives a call during the use of the application, and it ends up using resources unnecessarily, harming the user’s experience
Run automatically, you can see more information here
– Denis Rudnei de Souza
As Denis said, all life cycle methods of an Activity are executed automatically, however you can override these methods. You can, for example, call methods in your app’s onResume among other things you find necessary in your app.
– Antonio S. Junior