Skip to Content

Fixing performance issues in Ubuntu 12.10 »
« Flyin menu using offsetLeftAndRight not preserving after a layout
submit to reddit

These were some of the bugfixes I had this week – that took a little while the get around, just because they were a bit less obvious.

Dont re-use RemoteViews in widgets

This causes each action applied to applied each time – for a RemoteViews object in a widget create a new instance each time – I had some large memory leaks due to strings being held in a re-used RemoteViews object.

https://groups.google.com/forum/?fromgroups#!topic/android-developers/qQ4SV5wL7uM

Making a StateListDrawable: ordering the states are added is important.

When making a StateListDrawable in code, the order that the states are added determines the evaluation order – the statelist drawable then sets the first state it sees above or equal to the level of the state. In the two examples examples, the top one works, but below the drawable will always display enabled (1) if the enabled drawable is added first (i.e. it appears not to reflect the states at all).

The code below displays the states correctly:

StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] { android.R.attr.state_selected },mSelectedDrawable);
sld.addState(new int[] { android.R.attr.state_pressed }, mPressedDrawable);
sld.addState(new int[]{android.R.attr.state_enabled},  mEnabledDrawable);// enabled (1)
sld.addState(new int[]{-android.R.attr.state_enabled}, mDisabledDrawable);//disabled

The next code below always displays the enabled state, as the enabled level is low(1), but is added first in the execution order.

Presumably the states are added to an internal array and so the first state that satisfies the StateListDrawable’s current state is used (i.e. even if its selected it is also enabled and because enabled is added before the selected state, the enabled drawable is used)

StateListDrawable sld = new StateListDrawable();
sld.addState(new int[]{android.R.attr.state_enabled},  mEnabledDrawable);// enabled (1)
sld.addState(new int[] { android.R.attr.state_selected },mSelectedDrawable);
sld.addState(new int[] { android.R.attr.state_pressed }, mPressedDrawable);
sld.addState(new int[]{-android.R.attr.state_enabled}, mDisabledDrawable);//disabled


No comments yet!

You can be legendary and add one ...

All spam will be deleted - don't waste my time and yours. Fackbook profiles posted will be reported to FB as spam accounts


Allowed HTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*

By submitting a comment you grant Sentinel Web Technologies a perpetual license to reproduce your words and name/web site in attribution. Inappropriate and irrelevant comments will be removed at an admin’s discretion. Your email is used for verification purposes only, it will never be shared.

Fixing performance issues in Ubuntu 12.10 »
« Flyin menu using offsetLeftAndRight not preserving after a layout