views:

437

answers:

1

Hi,

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...).
Since 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

A: 

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.

Janusz
I do intend to launch each category through an intent and get a new instance of my activity, BUT - am I ensured that when I hit the back button I will get the previously displayed category, not just visually but also with its data inside my adapter? Thanks for your reply.
Rob
Yes, if you hit back the previous activity will be resumed and the old object with the old adapter will be shown, if your adapter lives inside the activity and not the application.
Janusz
A-H-A... my adapter is not inside the activity but rather in a class (file) of its own. That is probably what causes my problem. Thanks a bunch!
Rob