0

I have an application that uses a custom class. When the app is started I populate an instance of this class in the main activity with all the data it will hold.

Basic data is then shown in a ListView from which you can select a ListView item to see further information displayed in another Activity.

Currently, I am having to pass the data that is relevant to the new Activity to it by using:

intent.putExtra("NAME", value);

I want to implement a ViewPager so the user can easily switch between ListView items. Therefore the currently use method isn't very good as I only have the data for one entry at a time and would need to get back to the original Activity to get all the data again.

Is there a way to have my class objects globally available ANYWHERE in my application? I feel my applications code is getting bloated as I am overcoming these issues in bad code methods.

I've just looked into using:

MyApp myapp = ((MyApp)context.getApplication());

but this won't work unless I can pass the context around, which I'm not sure how to do??

In C# you'd create a static class that could handle this....

Thanks Neil

MARS
  • 101
  • 9
neildeadman
  • 3,974
  • 13
  • 42
  • 55

2 Answers2

0

You could have:

  • a static class holding your data (available until the process is killed)
  • a local service offering access to your data
  • the application class holding your data
  • a database mechanism to persist your data (in that case you'd simply pass IDs as intent extra)

All those options are more or less valid, it also depends on what you do with your data, where it comes from, ...

Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
0

I think you are looking for a Singleton object. Create the custom class that holds all the data as a singleton object and access its data from anywhere in the code. Also, as this data may not be directly related to the UI, you should not be associating it with an Activity.

HTH,

Akshay

Akshay
  • 5,747
  • 3
  • 23
  • 35