Life Cycle of an Activity. Doubts

Asked

Viewed 200 times

0

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

  • Run automatically, you can see more information here

  • 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.

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 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.