I am trying to come up with the best solution for a problem: I am developping an app with 3 tabs. I get xml data from a rest service and parse it into an object (there is only one request). The 3 tabs now display different parts fo this data. I was thinking about splitting the app into different activities to make the code more readable. How do I share the date between the activities? I know this question was posed a million times but I still cannot come up with a solution.
application object needs to derive from the Application class, but my main activity is already derived from the TabActivity class. use a different main class und then start my tab class with an intent?
A HashMap of WeakReferences to Objects. Seems a waste of memory but would be possible.
Put all the code into one activity and be done with it.
Thanks for any help :)
Depending on the data, there are a number of ways of doing this. How big is the data? If it is text-only, and not insanely huge, it might not be a problem to keep it in memory throughout the application lifetime.
If you split your application into different activities, you have two choices:
getIntent().getExtras()
.As stated, the right solution depends on what your data looks like. If all of your activities display different parts of the data, I would probably keep it in my Application subclass. However, if your application is structured in a similar way as a contact list (one activity for displaying the contacts, and one activity for detailed information about a contact), I would probably have my data in the main activity and pass just the necessary details to my other activity.
The ideal solution is that a the activity notifies a service which starts and handles the rest request, saves the result somewhere such as a sqlite-db, then the service notifies the activity that the transaction is done so it can query for the data.
But you only have one request and I don't think you'd bother doing all those mentioned above, so I'd go for number 3.
Data:
Setting the data in the oncreate() method of one activity:
Getting it in the oncreate() method of another:
In the android manifest:
The Class Data must be put there. That was rather simple. Formatting is a bit messed in this forum. I don't quite get it with the code format. :( Thanks for all the help.
In all my applications I'm using a Context class (called through a Singleton) that keep all application level informations and data that have any reason to be shared through the different activities.
By the way, this introduce a model level (in the MVC sense of it) in your application, in software design this part should be used to keep data that represent user's data and the application's state.
Singleton example :
When you got your data from the web (here username) :
To get it in one of your activity :
PS1 : extending application for satisfying such a purpose doesn't seem to be a good thing to me. Extending Application class is supposed to extend the normal application behavior, no to be a mean of storing common data.
PS2 : your weak reference map could be added in the Context object to structure your data
Try creating the object with
public static Object....
this static object can be used for all your classes, u can access the object bye ClassName.objectName