0

I'm trying to create a gesture detector class by following the guide on the Android Developers website, but I'm stuck because I cannot invoke the method onBackPressed() inside the new private class created by me. How can I manage it? This is my code:

    class DetailActivity : AppCompatActivity() {

    private lateinit var mDetector: GestureDetectorCompat

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.detail_activity)

        mDetector = GestureDetectorCompat(this, MyGestureListener())
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        mDetector.onTouchEvent(event)
        return super.onTouchEvent(event)
    }

    private class MyGestureListener : GestureDetector.SimpleOnGestureListener() {

        private val SWIPE_DISTANCE_THRESHOLD = 100
        private val SWIPE_VELOCITY_THRESHOLD = 100

        override fun onDown(event: MotionEvent): Boolean {
            //CALL onBackPressed
            return true
        }

        override fun onFling(
                event1: MotionEvent,
                event2: MotionEvent,
                velocityX: Float,
                velocityY: Float
        ): Boolean {
            //TODO
            var distanceX = event2.getX() - event1.getX()
            var distanceY = event2.getY() - event1.getY()
            if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (distanceX > 0)
                //CALL onBackPressed
            }
            return true
        }
    }
}

New code:

    class DetailActivity : AppCompatActivity() {

    private lateinit var mDetector: GestureDetectorCompat

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.detail_activity)

        mDetector = GestureDetectorCompat(this, MyGestureListener())
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        mDetector.onTouchEvent(event)
        return super.onTouchEvent(event)
    }

    override fun onBackPressed() {
        super.onBackPressed()
    }

    inner class MyGestureListener : GestureDetector.SimpleOnGestureListener() {

        private val SWIPE_DISTANCE_THRESHOLD = 100
        private val SWIPE_VELOCITY_THRESHOLD = 100

        override fun onDown(event: MotionEvent): Boolean {
            System.out.println("LOGG")
            this@DetailActivity.onBackPressed()
            return true
        }

        override fun onFling(
                event1: MotionEvent,
                event2: MotionEvent,
                velocityX: Float,
                velocityY: Float
        ): Boolean {
            var distanceX = event2.getX() - event1.getX()
            var distanceY = event2.getY() - event1.getY()
            if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (distanceX > 0) {
                    System.out.println("LOGG")
                    this@DetailActivity.onBackPressed()
                }
            }
            return true
        }
    }
}

EDIT: I added a new method onBackPressed and I invoked it through the inner class, but nothing changed. I also doesn't see the prints in my Logcat.

Babbara
  • 448
  • 1
  • 6
  • 21
  • Only `class` implies to static nested class and `inner class` implies to non-static nested class in kotlin . [See the Doc](https://kotlinlang.org/docs/reference/nested-classes.html). – ADM Mar 19 '20 at 14:24
  • I did it but it doesn't works. I updated the question with the new code – Babbara Mar 19 '20 at 14:45

1 Answers1

2

Make it a private inner class.
In Kotlin nested classes are static by default. To make it an inner class, which can access properties of the enclosing class, you have to use the 'inner' keyword.

See Java inner class and static nested class

RobCo
  • 6,240
  • 2
  • 19
  • 26
  • I declared it as **inner**, then inside the two methods I added **this@DetailActivity.onBackPressed()** and a print to check if the programs enters inside these methods but even if it compile without errors nothing gets printed and the onbackpressed doesn't works. – Babbara Mar 19 '20 at 14:42
  • If it compiles, then this question is answered. That the logic doesn't work as expected is something else to debug that I cannot answer immediately. Does the onFling method get called? Are you sure you should always return true? sometimes this means no further events are passed. – RobCo Mar 19 '20 at 14:49
  • The methods inside the inner class doesn't get called. I tried to put prints to debug everywhere in the code but nothing works – Babbara Mar 19 '20 at 15:08
  • Possibly the touch event is already claimed and does not bubble back to the Activity `onTouchEvent`. See https://stackoverflow.com/questions/15740948/activitys-ontouchevent-is-not-being-called-while-clicking-on-button – RobCo Mar 19 '20 at 15:17