Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old January 27th, 2012, 10:57 AM   #1 (permalink)
Junior Member
 
jonathanrz's Avatar
 
Join Date: Jan 2012
Location: Brazil - Blumenau, SC
Posts: 69
 
Device(s): Motorola Defy - Gingerbread(2.3.3)
Thanks: 12
Thanked 5 Times in 4 Posts
Send a message via MSN to jonathanrz jonathanrafaelz
Question Where declare the keys of the bundles

Hi

I have a little doubt, I spend data from Activity A to B with a bundle in the intent.

Where I should declare the keys? In Activity A or B?

Only to avoid confusion, I'm talking about the string that defines the data that will be set in the Bundle.Ex: public static final String KEY = "MyKey";

I declare always in the Activity A, but I think that if I could let all the keys in a file would be better, like keys.java or something like that.

Obviously, this is nothing that is generating errors, just a doubt about convention, a thing that I consider very important in software development.

jonathanrz is offline  
Reply With Quote
Sponsors
Old January 29th, 2012, 11:46 AM   #2 (permalink)
Senior Member
 
Join Date: Jul 2010
Posts: 977
 
Device(s): Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Thanks: 52
Thanked 199 Times in 144 Posts
Default

It should be a constant inside of the class that references it (In your case, Activity A).
jonbonazza is online now  
Reply With Quote
The Following 2 Users Say Thank You to jonbonazza For This Useful Post:
alostpacket (February 2nd, 2012), jonathanrz (January 29th, 2012)
Old February 1st, 2012, 09:57 PM   #3 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,090
 
Device(s): GalaxyNexus(LTE), NexusOne, OG Droid, GalaxyTab 10.1(LTE), Eris, Logitech Revue (fishtank)
Thanks: 4,164
Thanked 3,126 Times in 1,292 Posts
Default

Actually this is pretty subjective.

Jon's answer isn't wrong though -- it's just one way to do it.

Having a Keys file is OK too. Depending on the situation.

Another way would be to have an object that is passed between A and B as the place where the string is defined. In Android these are usually Bundles

There are a lot of strong opinions and years of advanced research about how to structure a program, but three things worth taking a look at are the concepts of cohesion, coupling and encapsulation.

Here's some more from Google on the subject too:
Android Application Framework FAQ | Android Developers
alostpacket is offline  
Reply With Quote
Old February 2nd, 2012, 03:20 AM   #4 (permalink)
Junior Member
 
jonathanrz's Avatar
 
Join Date: Jan 2012
Location: Brazil - Blumenau, SC
Posts: 69
 
Device(s): Motorola Defy - Gingerbread(2.3.3)
Thanks: 12
Thanked 5 Times in 4 Posts
Send a message via MSN to jonathanrz jonathanrafaelz
Default

Quote:
Originally Posted by alostpacket View Post
There are a lot of strong opinions and years of advanced research about how to structure a program, but three things worth taking a look at are the concepts of cohesion, coupling and encapsulation.
Define a file to the keys will be better for cohesion and worst for encapsulation.

Do you think that exist a weight for each item?
jonathanrz is offline  
Reply With Quote
Old February 2nd, 2012, 03:09 PM   #5 (permalink)
Senior Member
 
Join Date: Jul 2010
Posts: 977
 
Device(s): Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Thanks: 52
Thanked 199 Times in 144 Posts
Default

Like alostpacket mentioned, this is almost entirely subjective. My previous replied is a common (I don't really want to say standard, but...) approach as it provides a good balance of readability, maintainability, and compactness.

With that said, the "best" approach is whatever approach your designers and leaad developers tell you. If you are the only person working on the project, then in all honesty, it doesn't really matter. Just pick an approach that is most attractive to you.
jonbonazza is online now  
Reply With Quote
The Following User Says Thank You to jonbonazza For This Useful Post:
alostpacket (February 2nd, 2012)
Old February 2nd, 2012, 04:07 PM   #6 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,090
 
Device(s): GalaxyNexus(LTE), NexusOne, OG Droid, GalaxyTab 10.1(LTE), Eris, Logitech Revue (fishtank)
Thanks: 4,164
Thanked 3,126 Times in 1,292 Posts
Default

^ exactly


Sorry if I confused the issue a bit there Jon's answer is a perfectly good one.

And (as he also says) if you are the only dev, then it's fine to put it wherever makes you feel comfortable.

I just wanted to expand on the answer a little bit and give you some tips on some things to read as you learn over time. No need to have perfect organization the first time you write something.

Coding is about learning concepts and best practices while managing your time, and getting your product finished.

So I just wanted to give you some ideas that you may want to read up on as you progress.




Also yes, you are correct in that sometimes coupling and cohesion can seem like opposites, but the goal is to make them work together.

I think a good easy way to help yourself organize things in the beginning is to ask yourself
Quote:
if I put XYZ here, will the next programmer:

1) know what it means at a glance
2) know where to find it
3) be able to change it without breaking things or having to change many other parts of the program
These are the concepts behind the fancy words like cohesion, etc.

And most likely you will often find the "next" programmer to look at your code will be you, in 6 months or a year (when you have completely forgotten what the code does or how it works).

So it helps if you try and code in a way where you keep in mind the "next guy".

hth
alostpacket is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 12:10 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo