Joaquim Antonio Kapel
Joaquim Antonio Kapel

Reputation: 336

How to properly implement an interface in android fragment?

folks. I have been working on a project with kotlin and I need to make a fragment that comunicate with the parent activity... I followed exactly what google and other websites suggested but I still get an error "activity does not override anything"... All of the other solutions are not working for me... here is the code .

FRAGMENT

package com.me.assistan.assistant

import android.app.Activity
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Context
import android.content.Intent
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.LinearLayout
import android.widget.ToggleButton
import kotlinx.android.synthetic.main.content_newplan.*
import java.util.*

class addingTask : Fragment(), View.OnClickListener{
    var func = Functions
    var globalTask = GlobalTask
    private lateinit var listener: OnTimeSettedListener
    override fun onAttach(context: Context?) {
        super.onAttach(context)
            if (context is OnTimeSettedListener) {
            listener = context
        } else {
            throw ClassCastException(context!!.toString() + " must implement 
            OnTimeSettedListener.")
        }
    }
    companion object {
        fun newInstance(): addingTask {
            return addingTask()
        }
    }
    override fun onCreateView(inflater: LayoutInflater?, container: 
        ViewGroup?,
                          savedInstanceState: Bundle?): View? {
        val view: View = inflater!!.inflate(R.layout.fragment_adding_task, 
        container,
        false)
        val activity = activity
        view.theTime.setOnClickListener { v ->
            listener.onTimeSetListtedener("test")
        }
        return view
    }
    interface OnTimeSettedListener{
        fun onTimeSetListtedener(comic : String){
            println("ok")
        }
    }
}// Required empty public constructor

And not the MAIN ACTIVITY

class Newplan : AppCompatActivity(), addingTask.OnTimeSettedListener {
 var posx = 0f
private var startx = 0f
private var posy = 0f
private var starty = 0f
var backIntent = Intent();
var func = Functions
var globalTask = GlobalTask
val fragment = addingTask()

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

    if(savedInstanceState === null){
        var args = Bundle()
        supportFragmentManager.beginTransaction()
                .add(R.id.taskMain, addingTask.newInstance(),"newTask")
                .commit()
    }
}
 override fun onTimeSettedListener(comic : String){
    println("params")
}
}

I get the error on the activity class... When I remove the "override?, the error is gone but nothing happen when I click on the button... What am I doing wrong?

Upvotes: 1

Views: 8058

Answers (2)

albodelu
albodelu

Reputation: 7971

As @chris commented, you need to move the lines below outside of onCreate() method:

 override fun onTimeSettedListener(comic: String) {
    println("params")
}

You also need to match names replacing

interface OnTimeSettedListener {
    fun onTimeSetListtedener(comic : String){
        println("ok")
    }
}

by

interface OnTimeSettedListener {
    fun onTimeSettedListener(comic: String) {
        println("ok")
    }
}

Update

If you fix the name typo and remove the default implementation of the onTimeSettedListener declaration in the interface inside your fragment, as your activity implements it and Android Studio warns you about the missing override, it's possible to click it and select that the IDE implements it for you to avoid errors doing it:

interface OnTimeSettedListener{
    fun onTimeSettedListener(comic : String)
}

You also need to fix the call replacing:

listener.onTimeSetListtedener("test")

by

listener.onTimeSettedListener("test")

Upvotes: 1

Ergin Ersoy
Ergin Ersoy

Reputation: 889

I think you shouldn't add method body to your interface method. It is not allowed in Java. In Kotlin there is no error that showing method body is restricted. But you should remove body. Change your interface like

interface OnTimeSettedListener{
    fun onTimeSetListtedener(comic : String)
}

Also actually you are not overriding your Listener's method. Method name in OnTimeSettedListener is onTimeSetListtedener but you are overriding onTimeSettedListener which is not really exist in your code.

Also as @albodelu mentioned answer and @chris mentioned in comments, you cannot write methods in methods. It is not correct usage.

Upvotes: 1

Related Questions