Passing objects to new intents

by John C. Bland II » Tue, 30 Nov 2010 02:30:19 GMT


Sponsored Links
 I've googled this quite a bit and have seen many arguments to not pass the
object rather store it in a database, use a custom Application object, or a
static variable.

Ultimately, the app will use a database so I'll just pass the id of the
record and pull it out in the new intent [correct me if that's not a solid
direction] but for now I am strictly looking to learn.

Should I continue to explore passing a full POJO to an intent, passing each
individual value as an extra [prefer not], or stick with the database
approach?

Thanks folks!

---
John C. Bland II
Blog:  http://www.johncblandii.com 
Company:  http://www.katapultmedia.com 
Twitter: @johncblandii

-- 



Re: Passing objects to new intents

by TreKing » Tue, 30 Nov 2010 02:41:38 GMT


 On Mon, Nov 29, 2010 at 10:09 AM, John C. Bland II





Objects that I need to pass across Activities or Services I make
"bundleable", which just means they can contain two functions to store their
state to a bundle then restore it from a bundle.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 


Sponsored Links


Re: Passing objects to new intents

by John C. Bland II » Tue, 30 Nov 2010 11:29:28 GMT


 So you return a Bundle and receive a bundle then expand it to the
properties?

That's pretty quality. Good thought.

---
John C. Bland II
Blog:  http://www.johncblandii.com 
Company:  http://www.katapultmedia.com 
Twitter: @johncblandii







-- 



Re: Passing objects to new intents

by TreKing » Tue, 30 Nov 2010 11:43:18 GMT


 




Yup, basically. It's like extending the Parcelable interface ... only
without the headaches.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: Passing objects to new intents

by Doug » Tue, 30 Nov 2010 15:11:28 GMT


 



I make my objects Parcelable and "bundleable".  Parcelable is not such
a headache once you get the boilerplate code out of the way.  Then you
just attach the bundle already being made to the parcel.  Parcelable
is an advantage if you need to pass arrays of such objects around.
You can't attach arrays of Bundles to intents without your own
mechanisms.

Doug

-- 



Re: Re: Passing objects to new intents

by TreKing » Wed, 01 Dec 2010 00:38:01 GMT


 


It's not only the repetitive "CREATOR" stuff. I found that if I send a
custom parcelable to a Service which was to start after my main Activity had
gone away, loading my object would fail (class loader not available or
something ... there was a thread or two about it a while ago). This was my
major motivation for ditching parcelable.

Parcelable is an advantage if you need to pass arrays of such objects

Sure you can. Bundle extends parcelable, so you can attach an array of
parcelables that are Bundles.
Or, better yet, you can pass an ArrayList of Bundles with
putParcelableArrayListExtra(), which takes advantage of the generics
interface.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: Passing objects to new intents

by John C. Bland II » Wed, 01 Dec 2010 01:23:05 GMT


 Ok...I'll look into it. Thx dude!

---
John C. Bland II
Blog:  http://www.johncblandii.com 
Company:  http://www.katapultmedia.com 
Twitter: @johncblandii







-- 



Re: Passing objects to new intents

by John C. Bland II » Fri, 03 Dec 2010 06:20:18 GMT


 Just reporting back. It works perfectly. I had a couple glitches but that
was bad coding. :-D

Thx guys!

---
John C. Bland II
Blog:  http://www.johncblandii.com 
Company:  http://www.katapultmedia.com 
Twitter: @johncblandii


On Tue, Nov 30, 2010 at 11:22 AM, John C. Bland II






-- 



Re: Passing objects to new intents

by Dev Android » Mon, 06 Dec 2010 03:09:56 GMT


 can you please paste here code snippet.









-- 



Re: Passing objects to new intents

by Neilz » Mon, 06 Dec 2010 04:42:36 GMT


 In my latest app I had a lot of activities, and had to pass a lot of
objects between them. So, I came up with a different solution.

I created a base activity, common to the application, and made all my
other activities extend my base activity.

1) This is useful as you can put a lot of common methods and vars in
there,,,

2) I created a load of static objects which, when launching my
intents, I populate the static objects so that when the next activity
is launched they're there ready. No bundles, no parcelables, simple.

Now maybe this is obvious, but I must say I haven't seen this in any
other examples. I'm wondering if there's something 'wrong' with  this,
any reason why this shouldn't be done?

-- 



Re: Re: Passing objects to new intents

by Mark Murphy » Mon, 06 Dec 2010 04:55:45 GMT


 


1. Watch for memory leaks. Do not put things in static contexts that
might reference another Activity, for example (e.g., a widget).

2. Since Java does not support multiple inheritance, the base class
may interfere with uses of other activity classes (e.g.,
PreferenceActivity, MapActivity), or require code duplication.

Rather than having a "load of static objects", you could have just one
singleton that itself holds onto a "load" of other data. That will
make it easier for you to migrate to another approach (e.g., Service,
custom Application) if you so choose to later. It also consolidates
your initialization, makes it a bit easier to give the static data
more smarts about its static-ness (e.g., LRU algorithms to minimize
memory creep), etc.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://github.com/commonsguy 
 http://commonsware.com/blog  |  http://twitter.com/commonsguy 

Android App Developer Books:  http://commonsware.com/books 

-- 



Re: Re: Passing objects to new intents

by TreKing » Mon, 06 Dec 2010 05:35:46 GMT


 



As Mark said you can't really do this if you use any of the derived Activity
classes (List, Tab, and especially Map). For this kind of "common activity
stuff" I created a proxy of sorts - a class that takes an instance of an
Activity and implements functions that are common for all. Then each
Activity just creates an instance of this proxy with itself and uses it to
do the common logic stuff. It's a little more verbose, but works to
centralize the common code given the restrictive inheritance hierarchy.

2) I created a load of static objects which, when launching my intents, I

What happens when:
You start Activity A which initializes a bunch of static objects.
initialized by A.
You press Home and do other things for an hour or so, eventually your app
gets wiped from memory, including the statics.
You come back to your Activity, which will start at B since that was the
last thing the user saw.
A will NOT be called to initialize your statics and B will be using default
or non-initialized static data.

I would be very careful with using static data to store instance state
across Activities.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: Passing objects to new intents

by Neilz » Mon, 06 Dec 2010 06:14:20 GMT


 Cheers Mark and TreKing for your comments.

Interesting... I think for this particular app my model works
particularly well. I don't need to use any of the specialist Activity
classes (Map, List etc) so that problem never occured to me. But of
course for most apps this would be an issue.

Also, my app is storing objects that have been retreived from server
XML, which the user can refresh manually. So each time the activity
resumes, it checks to see if the static object exists... if not it
reloads it from the server.

Horses for courses and all that, it works well in this instance - but
in future I'll probably do as you suggest TreKing.

-- 



RE: Re: Passing objects to new intents

by maomaostevencao » Wed, 29 Dec 2010 13:53:06 GMT


 As Mark said you can't really do this if you use any of the derived Activity
classes (List, Tab, and especially Map). For this kind of "common activity
stuff" I created a proxy of sorts - a class that takes an instance of an
Activity and implements functions that are common for all. Then each
Activity just creates an instance of this proxy with itself and uses it to
do the common logic stuff. It's a little more verbose, but works to
centralize the common code given the restrictive inheritance hierarchy.

Interesting.But I have a question.
If I want to load a new activity and pass some of arguments using this way,
I have to get the reference of this new activity, but seems platform doesn't
provide any API to get it. How can you do it? I mean how I can get the
reference of the new activity.
 
thanks.

  _____  

From: android-developers@googlegroups.com
[mailto:android-develop...@googlegroups.com] On Behalf Of TreKing
Sent: Monday, December 06, 2010 5:35 AM
To: android-developers@googlegroups.com
Subject: Re: [android-developers] Re: Passing objects to new intents






1) This is useful as you can put a lot of common methods and vars in
there,,,



As Mark said you can't really do this if you use any of the derived Activity
classes (List, Tab, and especially Map). For this kind of "common activity
stuff" I created a proxy of sorts - a class that takes an instance of an
Activity and implements functions that are common for all. Then each
Activity just creates an instance of this proxy with itself and uses it to
do the common logic stuff. It's a little more verbose, but works to
centralize the common code given the restrictive inheritance hierarchy.



2) I created a load of static objects which, when launching my intents, I
populate the static objects so that when the next activity is launched
they're there ready. No bundles, no parcelables, simple.



What happens when:
You start Activity A which initializes a bunch of static objects.
initialized by A.
You press Home and do other things for an hour or so, eventually your app
gets wiped from memory, including the statics.
You come back to your Activity, which will start at B since that was the
last thing the user saw.
A will NOT be called to initialize your statics and B will be using default
or non-initialized static data.

I would be very careful with using static data to store instance state
across Activities.

----------------------------------------------------------------------------
---------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ;  - Chicago
transit tracking app for Android-powered devices



-- 


-- 



Re: Re: Passing objects to new intents

by TreKing » Thu, 30 Dec 2010 07:56:01 GMT


 On Tue, Dec 28, 2010 at 11:52 PM, maomaostevencao <





You do not need a reference to the new activity. You launch an intent with
the proper variables, then the new activity is launched and within it you
use getIntent() to get the Intent that launched it, including any variables
you passed along.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Other Threads

1. ListView Urgent Help Req :)

Hi guys,
I am developing a music application where there will be almost 30 to
35 lists .
Example ..
A list on page one say albums , artists  etc etc
Then if you select one list item say Albums , you go on a new page
with Album list, when u click on one album listitem , u enter song
list.. that way .
each list item has an image associated with it . And the text for the
listitem may be 2 lines per item. And the text should be scrollable.
Now my idea is to create a generic list which is common to all the
listview screens.
At runtime i should be able to pass the contents of each list to this
generic class.
What approach should i take? Can a XML layout solve this purpose..
Please give me some guidelines. Reuse is my main aim.. And for each
listitem i should be able to handle click listeners..
Please give me some guidelines.
Thanks in advance,
Alok.

--~--~---------~--~----~------------~-------~--~----~

2. Help with UI XML

Hi,

I need to create a view in GridView which will have a checkbox over an
image (on the bottom right corner). so, when someone selects that
image, the checkbox is checked. I know I will have to create a custom
adapter but how do I write the XML for the cell?

Thanks in advance,

Moazzam
--~--~---------~--~----~------------~-------~--~----~

3. trouble with programmatically adding mock location data

4. launch activity like phone call

5. Running camera in emulator using computer's webcam

6. Playing video in Android not showing the video, playing sound only with a still image

7. Dev phone delivery