Too many views in activity - android

I have an activity with more then 200 diffident views and layouts. Some of the views are images and some are texts or buttons. The visibility of the views changes from time to time, and so some of the textView contents, but nothing more.
The problem is with devices with big screens when the activity is out of focus and gets deleted from the stuck (see here under "Saving activity state" section). Unfortunately, to restore it with onRestoreInstanceState() , is a real mess.
I was wondering maybe I should not use Views but implement it in a different way in order to save memory?
Maybe just bitmap images on a Canvas or something else?
Any suggestion tricks, tips will be appreciated.

I suggest you to use Fragments, if you have a lot of views and layouts in one activity.

you can use any number of views as layout, you saying that it will change depends on time,
use setVisibility(View.Visible)
setVisibility(View.GONE)
setVisibility(View.INVISIBLE)

Related

Activity performance with conditional visibility gone/visible

I have a fragment that contains a LinearLayout that is conditionnaly displayed or not. In terms of performance what is the best? Mark the LinearLayout as android:visibility="gone" in the XML, and then in code I set its visibility if needed, or the opposite?
Inflating a layout with some views with a visibility set to GONE is bad for performances, especially if the view is a bit complex.
check url :http://magicmicky.github.io/android_development/benchmark-using-traceview/
Unless that layout is very complex and has too many views, I wouldn't worry about it, it won't matter very much.
But it does seem more logical to initially set it to be gone unless you decide that it should be visible afterwards.
You could also think of it from a UX point of view, should the user see it very briefly then it disappears? or should it work the other way around?
I think there is nothing much with the performance in these two implementation unless you are hiding the layout after a long running network calls or something like this which requires some time to make the decision of hiding and showing the layout or the layout is too complex.
However, I would suggest you to keep the following things in mind.
In case of orientation changes of your activity, you need to handle the layout visibility in your onCreate or onResume function as the activity gets recreated. If you are hiding the layout and not keeping a track of it in a global static variable, the decision might get lost and you might get inconsistency in your overall layout design. However, if you are hiding your LinearLayout based on a decision which is being made in your onCreate function, you will not face this exact situation.
If this LinearLayout is inside an item of your RecyclerView or ListView, think of handling each of your layouts items carefully.

Multiple Layouts with few repetitive views (here & there so, cannot use <include>) versus Fewer layouts with hidden views

I am not sure if this is even a question worth asking or I am just bit too much overworked to think through this hence, the community help would surely be great at this stage to resolve this issue.
So, my question is that I have some 20-25 screens in my Android app. The app overall comprises of navigation drawer, tabstrips, fragments and lists in most of those fragments (some fragments only have listviews while some fragments have several other components and listviews as well). Now, few of these listviews have slightly similar row views but, not such that I could use the tag "include" due to placement and size issues.
For example, in rowview_1, I have one imageview (ImageView_1) and three textviews (TextView_1, TextView_2, TextView_3) to its right and finally, an imageview (ImageView_2) on extreme right on whose click an event occurs. Whereas, in rowview_2, I have one imageview (ImageView_1) and three textviews (TextView_1, TextView_2, TextView_3) to its right. On extreme right, two more textviews which convey some extra info and an imageview (ImageView_2) beside ImageView_1 and below TextView_3.
So, what would be a better and optimized approach in such a scenario?
1) Having separate multiple rowview layouts
2) Having one rowview with all the static elements in one place and dynamically adding and removing the extra elements (I believe it could be equally expensive for a rowview in a listview).
I am not sure if there can be any other approach. Any idea/guidance is most welcome.
Thanks in advance!
First of all. I think the differences if there are any are very minimal.
Scenario 1:
1) Cleaner code
2) Easier to make changes to one without affecting the other.
Scenario 2:
1) Complicated layout file
2) Does not necessarily take longer to load if everything's visibility is set to GONE in the layout.
One thing to note is that if you are going to load both layouts in the same Activity, you should surely go with scenario 2. When you load it the first time, the activity caches the layout so the next time, it loads super fast. If they are split over different activity, then the cache is gone anyways. Another thing to consider is how often you make changes to both together. If that is going to be often then scenario 2 is better. Changes will be done in one file. I personally prefer scenario 1. Its easier to show someone else who wants to make changes.
Hope this helps.

Android layouts and screen rotation

Another problem that I encountered today...
I am looking for something similiar to flowLayout from swing, I was looking and found nothing...
I need some layout inside which I am gonna place 3 buttons, when there is not enough space ( In potrait mode for example) I want them to be shown like one button above taking 100% space of layout, the other two just under the first, when there is enough space (landscape mode) I want them to be placed with the same weights in one line.I thought about relative layout but it misses weight, linear layouts on the other hand doesnt fit my requirements from what I have read.
What you could try is overriding the onConfigurationChange method of the activity and try implementing some logic there to change the position of the views depending on the orientation of the device. Or in your oncreate you could instantiate all your views and hide a complete set of controls for one type of orientation and then on another type do the opposite. Ive done both of these and hiding and unhiding the views seemed to be the least performance taxing because everything was instantiated once in oncreate. You could also try using seperate layouts all together for each orientation which might decrease performance due to view inflation but if your views are not to complex you might not notice anything except on lesser devices.

Replacing views dynamically without changing activity

I would like to know how to go about doing this small problem that I am encountering while making a video player app.
On clicking the first control(the rectangular icon) in the above image the following view must be displayed instead of it which I am quite unsure as to how to do it. Here is what it is replaced by
Also please note, by any chance the activity should not be changed. I have been able to design the views individually but having problem changing them at runtime when user clicks. Could someone go about explaining as to how it can be done or provide some suitable links to achieve my goal. Thanks.
For something as simple as this you can just change the visibility of the views.
view.setVisibility(View.INVISIBLE)
Or the more effective:
view.setVisibility(View.GONE)
Do that on the views you want gone, I suggest a wrapper class. It's either this or changing the contentView as describded below.
this.setContentView(R.layout.newLayoutToUse);
However, I have a feeling there is a better way to do what you want. It's overkill to load a complete new layout if you just want to change the image of some buttons or imageviews.
This might be a stupid solution, 'cause i'm terribly tired right now, but why not use the bringToFront() method on the View that you want to display in the front? Display them both in front of each other, maybe in a RelativeLayout, and then swap between them as you wish.
They are small objects, so don't consume memory. I don't see why this shouldn't work.
OR
Place them above one another, so they overlap and then make the above view visible/invisible depending on which one you need to display.
OR
just remembered I read somewhere that you can scroll through a ScrollView automatically from code. So display both Views in a ScrollView in succession and when pressing the button or whatever, you scroll down to make the next menu visible. When pres back, you scroll up to make the previous thing available. Should work, and might also make a nice animation between changing of the menus.

Is it possible to reuse the views in ViewFlipper?

May be a dumb Question.But still, is it possible to reuse the views in viewflipper?
Now,i have three imageviews in a viewflipper.is it possible to have a single imageview and change the source to it?
You can probably reuse view if you want to take care of the bookkeeping your self. However the viewflipper requires at least 2 views. From the Android ViewFlipper Docs:
Simple ViewAnimator that will animate between two or more views
that have been added to it. Only one child is shown at a time. If
requested, can automatically flip between each child at a regular
interval.
You would have to remove the ImageView from the ViewFlipper and then put it somewhere else. You can't put it into two ViewGroups at the same time (you'll get an exception that the view has already a parent).
But this is an overhead which you simply don't need to do. Simply create new ImageViews and use them. The memory consuming part of an ImageView is not the object itself but the bitmap it draws so I really recommend to read this article.

Categories

Resources