views:

39

answers:

2

Hello all!

I am developing my first android app and need a little bit of help. I am creating a recipes page and I would like to have a Favorites button on it. I would like the user to be able to click the favorites button while they are viewing a recipe and have it added to the favorites page. However when the user clicks the button I would like them to be able to continue viewing that recipes page instead of moving to the favorites page.

I found out how to use intent to send data to another activity while changing the page. However I would not like the page to change.

I'm assuming I would be passing a button to the activity to do this(is there a better way?)

Thanks for all the help in advance!

A: 

You could collect all the favorites to a list and pass them all at once to the next activity when the user finally wants to switch the page. Or of course, you could simply make the list a public static member which the favorites can access, or - my preference here - save all the favorites with a 3rd party which can also handle permanent storage of it. That way you also isolate some dependencies: the recipes page doesn't and shouldn't need to care about who uses the favorites data and how. Likewise, I don't assume the favorites page is interested in when a favorite is pressed, but what favorites exist when it needs to show them.

totramon
A: 

You just need to persist the favorites list somewhere. Either in SharedPreferences or in a file, or in a database. On the button click add the item to the list. Your favorites activity then can use the list :)

Konstantin Burov