Life Cycle of an Activity. Doubts

0

The methods that compose the life cycle of an activity (onCreate, onStart, onResume, onPause, onStop and onDestroy), except for the onCreate method that is already inserted at the beginning of the application, are the others automatically called by the application? Or should the developer, using best practices, use them in their applications?

    
asked by anonymous 10.01.2018 / 20:25

1 answer

0

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

    
18.03.2018 / 19:05