开发者

Android: ListActivity design - changing the content of the List Adapter

I would like to write a rather simple content application which displays a list of textual items (along with a small pic).

I have a standard menu in which each menu item represents a different category of textual items (news, sports, leisure etc.). Pressing a menu item will display a list of textual items of this category.

Now, having a separate ListActivity for each category seems like an overkill (or does it?..)

Naturally, it makes much more sense to use one ListActivity and replace the data of its adapter when each category is loaded.

My concern is when "back" is pressed. The adapter is loaded with items of the current category and now I need to display list of the previous category (and enable clicking on list items too...).

Si开发者_如何转开发nce I have only one activity - I thought of backup and load mechanism in onPause() and onResume() functions as well as making some distinction whether these function are invoked as a result of a "new" event (menu item selected) or by a "back" press.

This seems very cumbersome for such a trivial usage... Am I missing something here?

Thanks, Rob


If the user hits the back button your Activity will likely get garbage collected. If you properly start your activity from the menu with the different categories through an Intent, with passing the category etc. and then choosing the content in the onCreate method, you will get a new Instance of your Activity every time the user chooses a category, that will be destroyed after the user hits the back button.

This behaviour is perfectly fine. You don't have to cope with strange error cases and populating the list will take some time so the object creation time of the new ListActivity will be no problem. Try to program it as easy as possible for you and then test if there is a performance problem in the end of doing so.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜