Android lifecycle: enter data into activity in onStart () or onResume ()?

If you receive data through the cursor and fill in the data on the screen, for example, set the window title in onStart() or onResume() ?

onStart() will seem like a logical place, because after onStart() activity can already be displayed , albeit in the background. It is noteworthy that I had a problem with a controlled dialogue , which made me rethink this. If the user rotates the screen while the dialog is still open, onCreateDialog() and onPrepareDialog() are called between onStart() and onResume() . If the dialog should be based on the data that is needed for the data before onResume() .

If I'm right about onStart() , then why does the Notepad example give a bad example by running it in onResume() ? See http://developer.android.com/resources/samples/NotePad/src/com/example/android/notepad/NoteEditor.html NoteEditor.java line 176 ( title = mCursor.getString... ).

Also, what if my activity starts another Actvity / Dialog, which changes the data that the cursor is tracking. Even in the simplest case, this means that I need to manually refresh my previous screen (the listener for the dialog in the main action) or, alternatively, I have to register ContentObserver, m no longer updates the data in onResume () (although I could update it twice) ?

I know this is a basic question, but dialogue only recently, to my surprise, made me realize this.

+7
source share
2 answers

To answer the question about NoteEditor, just take a look at the lines above the one you are quoting and you will see ...

  // Requery in case something changed while paused (such as the title) mCursor.requery(); 

Commentary seems to explain all this. Despite the fact that I myself did not consider the NotePad example, it seems that the author is building in the ability to recover from changes, while NoteEditor pauses (and then resumes).

As GSree explains (as I type this), there is no right or wrong answer, and it just depends on what needs to be done at what point in the Activity lifecycle.

+2
source

Again, the decision depends on what suits you.

If you want the cursor to be pre-filled once for each application (and not worry about any changes, you can do it in onCreate (). This method will be called only if the application process is killed and the application will be renewed.

If you want the cursor to be pre-filled each time the visible lifetime starts (in most cases, the service / broadcast causes your activity, you should use onStart ()

If you want the cursor to be pre-populated for each foreground life cycle, you should use onResume (). Therefore, if you have a dialog box or other subactivity that changes some information, and therefore you want to reload the cursor, it is best to do this in onResume (). The disadvantage of this method is that every time the action occurs in the foreground, the cursor reloads.

Hope this is clear.

+3
source

All Articles