Showing posts from January, 2015

Gesture detection and scrollview issue

In one of my latest application when I implemented ViewFlipper with gesture listener, I come across a strange issue. All child of ViewFlipper are scrollViews. I implemented simple gesture listener like when swipe to left or right will take to the next scrollView. But the issue was swipe functionality is not working in scrollView. If the ViewFlipper child are any other layout like Linearlayout, swipe functionality is working fine. As usual, I debugged and found that, we have to override the dispatchTouchEvent(MotionEvent e) of activity class.

The dispatchTouchEvent() will look like this,
public boolean dispatchTouchEvent(MotionEvent ev) { super.dispatchTouchEvent(ev); return gestureDetector.onTouchEvent(ev); } Where gestureDetector is an instance of android.view.GestureDetector class.


How to avoid MEMORY LEAKS in Android

Memory leaks means resources which are not available for garbage collection. GC will free resources which are not referenced. Sometimes a single reference can prevent a large set of objects from being garbage collected. For example, some static variables which are referenced in an activity may not be for GC, because as long as the reference exists, the Activity will be kept in memory, leaking all of its views.

Code snippet from Romain Guy is as,

privatestaticDrawable sBackground;@Overrideprotectedvoid onCreate(Bundle state){super.onCreate(state);TextView label =newTextView(this); label.setText("Leaks are bad");if(sBackground ==null){ sBackground = getDrawable(R.drawable.large_bitmap);} label.setBackgroundDrawable(sBackground); setContentView(label);} Here, the static drawable sBackground will leak.

The solution here is when the activity gets destroyed, release the drawable in destroy and unbind all callbacks

Here, our onDestroy() will look like this,

protected void…