Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Notepad tutorial - lifecycle - some work done twice?

According to the "Application Fundamentals" article, section "component lifecycle", onResume() is always called when a View becomes active, independent of the previous state.

In the Notepad tutorial, Exercise 3, I have found something confusing in NoteEdit.java:
There is a call to populateFields() in onCreate() as well as in onResume().
Wouldn't it be enough (or even better) to have it only in onResume() ?

In such a small example, it will not do any harm if populateFields() is performed twice, but in a bigger App, things can be different ...

Thanks and Regards,
Markus N.

like image 958
Markus N. Avatar asked Sep 15 '10 14:09

Markus N.


People also ask

What are the life cycle methods of Android activity?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.

What is onResume method in Android?

onResume() is called whenever you navigate back to the activity from a call or something else. You can override the onResume method similarly as onCreate() and perform the task.

What is activity and activity lifecycle?

Activity Lifecycle: Activity is one of the building blocks of Android OS. In simple words Activity is a screen that user interact with. Every Activity in android has lifecycle like created, started, resumed, paused, stopped or destroyed. These different states are known as Activity Lifecycle.

Which method is invoked when the activity is started?

The onCreate() method is where any initialization code should go, as this method always gets called after the activity has launched but before it starts running.


Video Answer


1 Answers

From a look at Notepad3, I would say you are correct. There doesn't seem to be any reason for them to call populateFields() in both onCreate() and onResume(). onResume is sufficient.

like image 100
Justin Breitfeller Avatar answered Sep 22 '22 06:09

Justin Breitfeller