1

I have created a method and an inner class in fragment. However outer class method is inaccessible in inner class of Fragment? How to call outer class method or member in inner class of fragment. Below is my code:

class BlankFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mOrientationListener = OrientationChangeListener(requireContext())

}

fun rotateUI(orientation: Int){
    /// something
}

/**
 * This callback returns rotation angle of the phone, to make it return orientation angles enable it on onStart and disable it onPause
 */
private var mOrientationListener: OrientationChangeListener? = null

internal class OrientationChangeListener (context: Context?) : OrientationEventListener(context) {
    /**      portrait
     * (0, 359)
     *
     * (270)            (90)
     * (land)           (land)
     */
    override fun onOrientationChanged(orientation: Int) {

            // this method is inaccessible
            //rotateUI(orientation)
    }
}}

1 Answer 1

2

For inner class the keyword should be inner not internal and then you can use this to access the method like :-

[email protected]
Sign up to request clarification or add additional context in comments.

2 Comments

Sandeep Kumar thanks for your answer. I have changed it to inner, it worked. rotateUI is still accessible without using this@BlankFragment indicator for inner class.
this@BlankFragment would only be needed if OrientationChangeListener itself had rotateUI member which shadows BlankFragment's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.