The methods related to the life cycle of an Activity are called automatically, even if you do not see them explicitly in your 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 you have 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 resource expenses, for example , when someone receives a call while using the application, and it ends up using features unnecessarily, damaging the user experience