Implementation of the Life Cycle of an Activity

Asked

Viewed 82 times

1

Hello. I am new to Android Development and would like to better understand how to build more robust applications for this platform. I understand how the life cycle of a Activity but wanted to understand how to better structure my applications following the best practices for such.

  1. Which of the life cycle methods should be used to build the Graphical User Interface? I mean, instantiate all the Views and assign their respective behaviors. Only the method onCreate or other methods may also be used?

  2. When should I use the onStart and the onResume?

1 answer

3


Basically, Activity’s lifecycle methods have their moments of execution.

See below:

onCreate(): It is the first function to be executed in an Activity. It is usually responsible for loading XML layouts and other boot operations. Is executed only once;

onStart(): It is called immediately after onCreate() - and also when a background activity is back in focus;

onResume(): Just like onStart(), it is called on the Activity startup and also when an Activity has focus again. What is the difference between the two? onStart() is only called when Activity is no longer visible and re-focuses, onResume() is called in the "focus resumes";

onPause(): It is the first function to be invoked when Activity loses focus (this occurs when a new Activity is started);

onStop(): It is only called when Activity is completely covered up by another Activity;

onDestroy(): The last function to be executed. After it, Activity is considered "dead" - that is, it can no longer be relaunched. If the user requests this Activity again, a new object will be built;

onRestart(): Call immediately before onStart(), when an Activity again focuses after being in the background.

Source: Article Understanding the life cycle of an Android app | DEVMEDIA

  • Thank you very much @Antóniomacave for the enlightening response.

Browser other questions tagged

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