Office Hours — Today, October 9

Friday, October 5

Oct 9
7:20 PM
Mark M.
has entered the room
Mark M.
turned on guest access
7:25 PM
Gabriele
has entered the room
Mark M.
howdy, Gabriele!
how can I help you today?
Gabriele
Hi :)
I pastebin some code, just a moment
7:30 PM
Gabriele
I'm using a webview inside a pager, it's working
My question is, is my "object oriented" concepct corret? Because to me, it seems that I have too much code in this class
Mark M.
194 lines isn't *that* huge
Gabriele
You're an experienced programmer and probably you wold move something, but I don't know which is the code the move, if I have to move
But, private void initializeWebView() is right in this class?
Or it would be better to put these things elsewhere?
Mark M.
well, if you were using fragments, all of that would be in the fragment
and your PagerAdapter would be a FragmentPagerAdapter and therefore already a bit simpler
however, I am assuming that you are intentionally avoiding fragments here (e.g., you want the ViewPager itself to be in a fragment)
Pedro T.
has entered the room
Gabriele
Yes, I've found some difficult using my first pager, so I thought it was better to not use fragments
Mark M.
you could create some sort of WebViewHelper class that handles all of those listener interfaces
and initializeWebView() would create an instance of the WebViewHelper
Gabriele
Ah, so a class WebViewHelper can contains my listeners
Mark M.
(BTW, howdy, Pedro -- I will be with you shortly)
Gabriele: right
7:35 PM
Mark M.
since those listeners are mostly interfaces, WebViewHelper could implement the interfaces directly on itself, rather than using a bunch of anonymous inner classes
however, WebViewClient is not an interface, so your WebViewHelper would probably extend WebViewClient
that might be worth the effort
but, in the end, it does not matter how many lines of code are in a file
what matters is whether or not you will understand what you wrote, three months from now :-)
Gabriele
I see. It wasn't for the number of lines, but becase I was scared of "how" my code was written. Am irespecting object oriented paradigm? This was the mainly reason. :P
Mark M.
unless this is a college homework assignment, I would worry less about "object oriented paradigm" and more on whether you understand the code
let me take a question from Pedro, and I'll come back to you in a bit
Pedro: do you have a question?
Pedro T.
Yes
Gabriele
Yes, sure.
Pedro T.
I still trying to solve why is my app with a heap
so In fact the cache
of the resources
is taking to much spaces
space
on the 4x lg hd
also more than the resolution
the object is being stored uncompressed
7:40 PM
Pedro T.
with 2160X1424
that is a lot more than the screen resolution
And also, each button with its respective selector
is being stored with more resolution than the screen needs
this also explains, why it crashes on my 4x HD, but doesn't on my old cell phones
so I was wondering if there is a way to use Recycle
for my xml bitmaps
that are not initialized under code
so after each screen I can free the memory
Mark M.
OK, first I need some clarification on some things that you wrote here that do not make complete sense
Pedro T.
ok
Mark M.
1. "the cache of the resources is taking to much spaces" -- do you mean Android drawable resources, the kind you would have in res/drawable-*/ ?
Pedro T.
yes
Mark M.
2. "the object is being stored uncompressed" -- what object?
7:45 PM
Pedro T.
the backgroung image
and the buttons
View paste

http://stackoverflow.com/questions/12748032/strange-android-view-infalteexception#comment17222753_12748032
there is the layout information
Mark M.
3. "for my xml bitmaps" -- what are "xml bitmaps"?
Pedro T.
drawables used as background that are managed using a selector
Mark M.
OK
an LG Optimus 4X HD should be an -xhdpi device
Pedro T.
yes
Mark M.
do you have a res/drawable-xhdpi/ directory with bitmap files in it?
Pedro T.
yes
Mark M.
do these images include the ones that you are complaining about (background and buttons)?
Pedro T.
yes
Mark M.
what resolution are the images in your project?
Pedro T.
1080 x 760 px
Mark M.
how did you determine that they are 2160X1424 in your heap?
Pedro T.
using MAT
Mark M.
MAT tells you bytes, not resolution
7:50 PM
Mark M.
regardless, to your question, you cannot recycle resources
Pedro T.
Hoooo I thought mHeight and Mwidth were the resolution
of the object
Mark M.
oh, that may be
Pedro T.
first
Mark M.
I thought you were doing a calculation based upon the amount of heap space the Bitmap object consumed
I do not know why they are being sized 2x in each dimension
but you will need to have fewer/smaller of these resources if that is the source of your difficulty
even 1080x760 is going to take up ~2MB or so of heap space
Pedro T.
exactly
Mark M.
and there is nothing you can do about that, other than to have fewer or smaller of those images
or move some out of resources, where you manually manage the memory yourself
for example, having a 1080x760 button background seems... excessive
Pedro T.
1080 x760 is the screen background
7:55 PM
Pedro T.
the buttons are 260 x74
Mark M.
OK, those should not be a problem, if there are not too many of them
at any rate, it is very difficult for me to help with this sort of question in these chats
Pedro T.
there are 8 of those buttons and if we include the selector they are 16
the first screen is ok
the problem is that after 2 random screens it crashes
each drawable is being stored on the heap
but you're right
Mark M.
all I can tell you is that if you are convinced, from MAT, that your problem is with your resources, you need fewer/smaller resources
Pedro T.
I will have to manage the dashboard from code
that is the main problem
Mark M.
let me swing back to Gabriele, and I will come back to you in a bit
Gabriele: do you have another question?
Pedro T.
ok
Gabriele
Another thing is when to use static objects in my classes. For example, I've declared my webView static, but I'm not sure which question I should answer, to know if it should be static or not. I thought: "I should not create another instance of webview, so I can declare it static". Is this assumption wrong? I know from Java that I should use static when the object belongs to the Class and not to the Object, but I can't think an example of this in android, because what the PagerAdapter represents? Does the webview belongs to the class or to the object? Can I have problems using static, due to android life cycle?
Mark M.
you should never declare widgets to be static
Gabriele
ah, why?
Mark M.
"Is this assumption wrong?" -- yes, because you are most certainly creating more than one WebView
you have more than one page in the ViewPager, and each page is a WebView
8:00 PM
Mark M.
hence, you have more than one WebView
8:00 PM
Mark M.
oh, no, wait
Gabriele
Hm. What do you mean more then one? If I create only one, in one page?
Mark M.
I see, you only have two pages
one WebView and one something else (for bookmarks)
Gabriele
It's a listview, the other.
Mark M.
"Does the webview belongs to the class or to the object?" -- the object
"Can I have problems using static, due to android life cycle?" -- absolutely
you are creating a memory leak, unless you arrange to null out that static reference somtime
er, sometime
your objective should be a project with zero static data members
static data members typically are a necessity, particularly in Android development
but the ideal Java project has no static data members at all
Gabriele
Last thing, to understand better: which is an example of static data member necessity in Android?
Mark M.
let's say that you are creating a camera app with a built-in image editor
you have one activity for the camera, another activity for the editor
when the camera activity takes a picture, while the picture needs to be stored on disk, it will also be needed in memory for the editor
and a bitmap of that size is too big to pass in an Intent extra between the activiteis
er, activities
your options are:
1. have the image editor read the photo back off of storage, which is slow
2. cache the photo in a static data member, so both activities have access to it
static data members, in Android, should principally be used for things that need to be accessed by multiple components (e.g., multiple activities), and then typically as an in-memory cache for some persistent backing store
Gabriele
Ah, so in this case, I'm using one-image-in-memory, to serve two and after I null the object?
8:05 PM
Mark M.
yes, eventually, the image editor activity would want to set the static data member to null, to allow the Bitmap to get garbage-collected
Gabriele
Ok, thank you.
Mark M.
Pedro: do you have another question?
Gabriele
See you the next time, bye all. Thank you Mark.
Gabriele
has left the room
8:10 PM
Pedro T.
no thanks
Mark M.
OK
if you come up with one in the remaining time, be sure to ask
8:30 PM
Mark M.
that is the end of today's chat
the next one is Thursday at 10am Eastern
have a pleasant day!
Pedro T.
has left the room
Mark M.
turned off guest access

Friday, October 5

 

Office Hours

People in this transcript

  • Gabriele
  • Mark Murphy
  • Pedro Teran