Java – Kotlin Android basic methods are not called

Kotlin Android basic methods are not called… here is a solution to the problem.

Kotlin Android basic methods are not called

I have a basic activity like this that has the abstract method abc().

abstract class Base: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        Log.i("abc", "onCreate base")
        abc()
    }

abstract fun abc()
}

MainActiviy extends Base

class MainActivity : Base() {
    override fun abc() {
        Log.i("abc", "method called from base")
    }

@Inject
    lateinit var mainPresenter: MainPresenter

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.i("abc", "onCreate")

App.appComponent.plus(MainModule(this)).inject(this)

button.setOnClickListener {
            mainPresenter.performToast(editText.text.toString())
        }
    }

fun showToast(string: String) {
        toast(string)
    }
}

When I run MainActivity, the log only shows “onCreate”. This means that Base’s onCreate is not called. Can you tell me why the basic method is not called?
It looks silly, but I tried, but the base was not called
THE SAME CODE WORKS IN JAVA

Solution

You did not override the same onCreate method in both classes. Looking at the documentation, it seems that one or the other will be called, depending on whether persistableMode is set to persistAcrossReboots. This means that no matter what you do in the subclass, the code in the Base class may never execute.

Related Problems and Solutions