Office Hours — Today, February 21

Yesterday, February 20

Mark M.
has entered the room
Feb 21
9:55 AM
Mark M.
turned on guest access
Ron B.
has entered the room
Mark M.
hello, Ron!
how can I help you today?
10:00 AM
Ron B.
Hi Mark. Trying to get an API key for MapsV2 that covers both debug and deployment. I'm on page 1035 of your 5.6 book. Don't see "Registered Apps" on the left navigation bar of the Google Cloud Services
Mark M.
yeah, they changed that site, again
(grumble, grumble, grumble)
hold on a second while I pull it up
click "Credentials" in the "APIs & auth" area
if you have enabled the Maps V2 API (in "APIs" under "APIs & auth"), you should have a red "CREATE NEW KEY" button
that will pop up a dialog where you can enter signing key/package name pairs
(after first choosing "Android key")
Ron B.
So far, so good. Got the enter pairs screen...
10:05 AM
Mark M.
fill them in, click "Create", and you're set
Ron B.
So I have to run keytool against my keystore(s)?
Mark M.
to get the signing key fingerprint, yes
that's covered in "Your Signing Key Fingerprint(s)", on the previous page of the book
I have the command there for the debug keystore; you'd substitute your own path, alias, and passwords for production keystores
10:10 AM
Ron B.
So I do this twice, once for debug keystore, once for deployment keystore and get a single API key back?
Mark M.
if "this" is "run keytool", then yes
you can paste in both pairs (debug fingerprint + package, production fingerprint + package)
this allows you to use one API for both builds
and saves having to mess around with swapping API keys between debug and production
which was a serious pain in various body parts back in the Maps V1 days...
Ron B.
Great. That's what I was hoping for. It will take me a while to work through it.
Today, I wish there were more people for you to talk to ....
Mark M.
it's an unusual time slot
most chats are Tuesday and Thursday
I am unavailable next week, so I added a third chat this week
10:15 AM
Mark M.
if you have another question, go right ahead
Ron B.
No, I'm all set for now. I have to work through getting the fingerprints. Not that urgent, if I have issues I can wait a couple of weeks. Thanks!!
10:25 AM
EGHDK
has entered the room
Mark M.
hello, EGHDK!
EGHDK: do you have a question?
EGHDK
Yep.
10:30 AM
EGHDK
I have two activities and one custom class on top of that in my app. When I go from activity A to B, I create a new CustomClassObject and I pass it a context. In this class, I use the context to finish activity B when something happens. My problem is that I'm using a static variable in activity B and even though I have finished activity B, if I go back, my static variable is still set to whatever I have changed it to. Hope that wasn't too confusing.
Mark M.
that's how static data members work in Java
what are you expecting to happen?
EGHDK
Okay, let me expalin again and what I thought was going to happen.
10:35 AM
EGHDK
Go from activity A to B. In B I have a static data member, that get's toasted when the activity Resumes (just so I can check the value of the static variable. I finish the activity, and I'm back at A. I open B from A again, and it still has that static member set to what it was before. In my head. The Activity was finished, so none of the variables in there should exist anymore, therefor when creating a new intent, I should get a new instance of this static variable.
Mark M.
why did you make it static?
the behavior you describe is for a *regular* data member, not a static one
sorry, I meant "the behavior you want"
EGHDK
Well, I need to access the static member from the customclass I told you about.
I kind of understand why the static variable sticks around... but that got me thinking. Does that mean that ENTIRE activity B is still in memory?
Mark M.
no, you need to access the *data* from the custom class
you *chose* to use some static data member to do that
10:40 AM
Mark M.
you are welcome to choose something else
for example, make it a *regular* data member, with a getter method (getWhateverTheHellThisIs())
then, have your custom class hold a reference to the activity
or, simply pass the data into the custom class in its constructor
and I'm sure there are other possible patterns, but since I do not know anything much about this "custom class", I cannot advise you further
in terms of whether your instance of Activity B is still in memory, I don't know, because I have no idea what this static data member is
EGHDK
It's just an int.
Mark M.
then that will not hold a reference back to the Activity B instance
and so the Activity B instance is not reachable from this static data member
EGHDK
But if it were something else... does that mean my instance of B would still be in memory?
Mark M.
it depends upon what "something else" is
EGHDK
Any examples?
Would it just have to be an object?
Mark M.
if "something else" holds a reference back to the instance of Activity B, then yes, the static data member will be able to reach the Activity B instance, and the Activity B instance cannot be garbage-collected
so, for example, if you inflate a layout in Activity B, and take a widget from that layout (findViewById()), and put that widget into a static data member, you're in trouble
because widgets hold the activity they belong to
and so the static data member references the widget, which references the activity
and therefore the activity cannot be garbage collected
EGHDK
How do I know what objects hold the activity they belong to?
10:45 AM
Mark M.
in most cases, because you wrote the class in question
this is why using static data members is risky
and needs to be done carefully
EGHDK
So lets say I want to use ActivityB.getMyInt() in my custom class. instead of using a static.
How would I know I'm using the correct instance of ActivityB?
Mark M.
well, where does this instance of this "custom class" live?
is it too a static data member somewhere?
EGHDK
The custom class was started in Activity B.
Mark M.
that does not really answer my question
is the instance of the custom class being referenced from a static data member of Activity B?
or a regular data member of Activity B?
EGHDK
Regular
Mark M.
then how would it ever know about any *other* instance of Activity B?
10:50 AM
Mark M.
you wrote the code
you see where you are teaching this custom class instance about the instance of Activity B
EGHDK
That's actually a great point.
Never thought of it that way.
"then how would it ever know about any *other* instance of Activity B?"
I always actually felt indifferent about doing that, because I thought it was creating a new object, and getMyInt from that new object.
That makes sense. Thanks Mark.
Mark M.
if anyone has another question, chime in
EGHDK
So in Activity B I have a method called getMyInt. In activity B I create an CustomClassObject(Context), and then in my CustomClassObject I simply do (ActivityB) ctx.getMyInt(); and that should be it.
Mark M.
even better would be to just hold onto ActivityB directly
rather than pretending it is merely a Context, then casting it back to an ActivityB instance
EGHDK
Aha. Even better.
Mark M.
but, yes, the mechanics are pretty much what you describe
10:55 AM
EGHDK
So my constructor in my class should be requesting an Activity as an argument or an ActivityB as an argument?
Mark M.
ActivityB
after all, you *need* it to be an ActivityB to be able to call getMyInt() on it
it does you no good to be passed some other Activity subclass
or some other Context subclass
so, you make as well make Java's strong typing work for you, to prevent bugs where you try passing in the wrong thing
EGHDK
Ah yes. It's all coming together now.
But even if I pass in an ActivityB as a context, and it's only asking for a context, then that activity I pass in, is STILL technically an Activity even though the constructor only asked for a context.
Mark M.
sure
EGHDK
Which is how it allows me to cast it back to an ActivityB if anything.
Mark M.
right
EGHDK
Great.
Making progress. haha. Thanks again Mark. Almost through my first java book. I can see clearly now.
The rain is gone.
11:00 AM
Mark M.
the rain
11:00 AM
Mark M.
is still here, though it will hopefully have passed by this evening
:-)
and that's a wrap for today's chat
the transcript will be posted to http://commonsware.com/office-hours/ shortly
EGHDK
Thanks. See you next week
Mark M.
the next one is not until March 4
March 4 at 4pm US Eastern Time, to be specific
have a pleasant day, all!
Ron B.
has left the room
EGHDK
has left the room
Mark M.
turned off guest access

Yesterday, February 20

 

Office Hours

People in this transcript

  • EGHDK
  • Mark Murphy
  • Ron Bruckman