bakusek
bakusek

Reputation: 135

Methods and class confusion

I've got two activity classes (one of it is in the same packet as Method class and second one is in another packet - but i think that that's no problem) and one helpfull class called Methods where i allocate variable declariations and getters and setters. When I want to store some data just like string array in first activity class i make something like this:

Methods met = new Methods();
met.StoreSomething[0] = ("qwerty")

and when i call to get it in same class it work well:

met.GetSomething[0].

Problem is when i want to get this data on second class. I declare it in the same way:

Methods met = new Methods();
met.GetSomething[0];

but after this i got null array. I think that when i declare in second activity class new met i this time i make new objects which are not link with objects frome first class. But i'm not sure of it. So anyone can help me? How to solve this problem?

Upvotes: 0

Views: 82

Answers (3)

Jef Vrijhoeven
Jef Vrijhoeven

Reputation: 102

If you want to access the same instance of Methods in both classes Activity1 and Activity2, then there are a few ways to achieve this goal:

But 1 remark before starting to create Methods isntances: In your question you say "When I want to store some data in first activity class ..". This is not correct. Yu are always working from an instance of a class. So, somehow you must have created an instance of Activity1 and an instance of Activity2.

If these 2 Activity instances can "talk" to each other, the first one that creates the Methods instance should send the reference to the second Activity instance. Then both Activity instances have a reference to the same Methods instance, and what one instance sets, can be retrieved by the other.

2) If the Activity instances can not talk to each other, maybe you can use a singleton approach: Make the constructor of Methods private, and add a static method to Methods like getInstance(). When getInstance is called the 1st time, it can create an instance of Methods, and store this in a static variable in Methods. The next call to getInstance just returns the same instance. As a result, both Activity instances have a reference to the same Methods instance, and they can set/get the same data.

3) If it is not desirable to have only 1 Methods instance, you can use a variant of the 2nd solution: Add a key to the getInstance method (which must be known to both Activity instances), and create a new Methods instance for each new key. If both Activity classes use the same key, they will receive the same reference, and can interchange data again.

4) You can make the methods "storeSomething" and "getSomething" in Methods static. Then all instances of Methods will use the same data, and your Activity instances can interchange data again.

I hope 1 of these slutions helps you solving your problem.

Upvotes: 2

Want2bExpert
Want2bExpert

Reputation: 527

Try to use SharedPreferences for simplicity to store and retrieve Data between Activities If you don't want to keep.the data after retrieving it in second Activity just remove it or clear your sharedPreferences obj.

Upvotes: 0

tahayk
tahayk

Reputation: 512

you can use static to share attributes between classes. Java for Beginners: Static Variables

Upvotes: 0

Related Questions