Sharing Objects between activities using Singleton ?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Sharing Objects between activities using Singleton ?

Postby kungfuslippers » Tue Apr 28, 2009 4:30 pm

Hi,

I'm a newcomer to Android and am still trying to get my head around the framework - so bear with me!

If you have say an application with a 'main' view and 2 different types of list 'view' which all need to be displayed independently, does each "view" have to be implemented by a separate class derived from Activity (or ListActivity in the case of lists) ?

More generally if I have an application with N Activities that all need access to an 'engine' class, I've read on other forums that I'd have to implement the Engine using a Singleton pattern to allow the different activities to access to the engine

Is this is the only/best/recommended way to do it in the Android? Are there alternatives - just out of interest? I understand that you can use Bundle to pass information between classes, but this doesn't apply to more complex objects. Using a contentProvider doesn't seeem appropriate for what I want to do either.

thanks
kungfuslippers
Freshman
Freshman
 
Posts: 2
Joined: Tue Apr 28, 2009 4:16 pm

Top

Postby padde » Tue Apr 28, 2009 5:41 pm

Well.. a singleton is a good and common way of serving those needs but there isnt a best way.. and to say whether this is recommended for your needs or not is hard to say without knowing what exactly you want to achieve :)
But i can say for sure that this isnt the only way. As far as i understand the architecture of services (haven't use them yet) i think running some sort of Engine Service thats running in background would be a way too. The other activitys just binding on that service and use it or interact with it with some kind of selfmade protocol.
I use the bundle way at the moment because its easy and fits my needs.. and i send a relative huge and complex
objekt in the bundle.
I just have implemented serializable interface in all the classes of the objekt and bundled it after that... just works perfect :)

And to the view question.. its up to you.. you can implement those lists as plain views and then hide and show them when you have to... or you use a own listactivity and start and close it when you have to.
The listactivity is just a convenient class for a simple way of creating an app that just needs a list or is based on a list.

Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby kungfuslippers » Tue Apr 28, 2009 7:32 pm

Thanks for the answer, I hadn't thought of using a service. You've given me some good food for thought.

nice one!

cheers.
kungfuslippers
Freshman
Freshman
 
Posts: 2
Joined: Tue Apr 28, 2009 4:16 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 4 guests