开发者

How to put the proper code for each android's life cycle?

What is differences between onCreate and onStart cycle ? I got confuse to p开发者_JS百科ut the proper code for these cycles. for example in my case, I have main activity listview to display data from database and other activity to create data to database. Activity to create data is called by listactivity. After creating data is succeeded, it would be back to listactivity. And the data in listview should be updated.

When I put initialization database, cursor and adapter on onCreate method, the listActivity wouldn't update the latest data after creation the data. But if I put it on onStart method, it's updated.

my question: Is it correct in this case to put all initialization on OnStart method ? Because I'm thinking it would be expensive to reinitialize for each database record to listview if there is one updated data.


What is differences between onCreate and onStart cycle ?

onCreate() is called when the activity is first created. onStart() is called whenever the activity becomes visible, which includes when it is first created (after onCreate()) and after it is coming back to the screen from being stopped (e.g., another activity took over the screen).

I have main activity listview to display data from database and other activity to create data to database. Activity to create data is called by listactivity. After creating data is succeeded, it would be back to listactivity. And the data in listview should be updated.

Use a managed Cursor, and that will happen automatically. See startManagingCursor() on Activity.


It depends. Do you want the data to be reloaded everytime the user comes back to the visible activity? If yes, you should add these methods in onStart(). If you are looking to load the values at the very beginning (more like a once off job) and do not want to be reloaded, then onCreate is the right place.

See doco for explanation of onCreate and onStart. This diagram in the link is useful http://developer.android.com/guide/topics/fundamentals.html#actlife

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜