The common way to use an object in different activities?

Put your problem here if it does not fit any of the other categories.

The common way to use an object in different activities?

Postby poffi » Fri Nov 27, 2009 11:36 am

Hello everybody

I started to programm android a month ago. Now i have a problem, for which i couln't find a solution until now.

My program has different activities. Some of them are loading big xml-files from the internet and parse them into Java-objects. The loading and parsing needs alot of time, so it is important that i don't have to load the files more than once. I like to use the created object in more than one activity.

QUESTION: How can i do that. Always when i change the activity using a Intent i lose all my objects. With the setExtra-methods i can "transport" values, but im not able to transport objects (sure, I could program a workaround, but that can be the solution). Another possibility is a static-var, but this is really bad as well, for the common reasons.

How can i do that or better, which is the common way to use an object in different activities?

Thanks!!

Greetz Poffi
poffi
Freshman
Freshman
 
Posts: 9
Joined: Fri Nov 27, 2009 11:26 am

Top

Postby Johan Degraeve » Fri Nov 27, 2009 1:04 pm

my answer should be in my next post
regards,

Johan
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

Postby Johan Degraeve » Fri Nov 27, 2009 1:04 pm

Hi,

I'm in the same situation as you, just started a few weeks ago developing for Android and I'm getting to the point where data needs to be passed from one activity to the other and back.

I think you need to use Bundles.
When starting an activity, one of the parameters is an Intent. While creating the Intent (before starting the activity) you can put Extras, which can take as parameter a Bundle. And while creating the Bundle, you can add objects, which seems not so easy for custom classes, here an example : http://www.anddev.org/putting_an_object ... t6431.html

Then you start the new Activity. In the calling activity you also override onActivityForResult which will be called when your second activity ends, and has again an Intent as parameter, which has a bundle ...

In your second activity, you als create a bundle with the response data you want to return, call
Activity.SetResult and Finish
regards,

Johan
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

Postby poffi » Fri Nov 27, 2009 2:43 pm

Thanks! Cool...a usefull possibility. I will it implement this way!

Would be nice, if another person (maybe a more experienced person) could confirm, that this is THE WAY to do it! Just for the reasons that i do it not on the wrong way. thx!
poffi
Freshman
Freshman
 
Posts: 9
Joined: Fri Nov 27, 2009 11:26 am

Postby Johan Degraeve » Fri Nov 27, 2009 3:12 pm

I think this is the way it should be done, I've been using the book Unlocking Android

but as you see there are some experienced guys here who can give more explanation.
regards,

Johan
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

Postby qlimax » Fri Nov 27, 2009 9:10 pm

I would remember that implementing the Serializable interface and passing the object via putSerializable / getSerializable, you will make a copy of the object. no reference will be passed.

:wink: watch out.
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Postby Johan Degraeve » Fri Nov 27, 2009 9:15 pm

qlimax wrote:I would remember that implementing the Serializable interface and passing the object via putSerializable / getSerializable, you will make a copy of the object. no reference will be passed.

:wink: watch out.


yes indeed, there seems to be no way to pass object references. But what would be the advantage ? performance ? I assume starting Activities is not something which is done very frequently.
regards,

Johan
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

Postby qlimax » Fri Nov 27, 2009 9:28 pm

no advantages or disadvantages... simply:
assume that you go from activity A to activity B and you pass an object.
then you modify the object when you are in B.
when you go back to A, the object you passed is not changed :? .
bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby appforce » Mon Nov 30, 2009 10:01 am

Hi,

If you want to avoid copying the objects, you can use your Application class to exchange objects. Extend the Application class, put it in your Manifest and put a HashMap<String, Object> in it. Now in any Activity you can do something like that:

((MyApplication)getApplication()).getObject(MY_KEY); // or put the same way

where MY_KEY is a String as you normally use keys in Intent extras.

AppForce
appforce
Experienced Developer
Experienced Developer
 
Posts: 60
Joined: Mon Nov 23, 2009 8:28 pm

Postby virtual » Mon Nov 30, 2009 1:50 pm

Yes I am also using Application class to keep all my xml parsed data in static ArrayList<> in the Application Class.
Regards,
Virtual
virtual
Developer
Developer
 
Posts: 26
Joined: Tue Mar 24, 2009 11:20 am

Postby qlimax » Mon Nov 30, 2009 1:53 pm

appforce wrote:Hi,

If you want to avoid copying the objects, you can use your Application class to exchange objects. Extend the Application class, put it in your Manifest and put a HashMap<String, Object> in it. Now in any Activity you can do something like that:

((MyApplication)getApplication()).getObject(MY_KEY); // or put the same way

where MY_KEY is a String as you normally use keys in Intent extras.

AppForce



Personally, what I did in this case, was writing my own data model class, and using a singleton for it.

bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Return to Other Coding-Problems

Who is online

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