coinhndp
coinhndp

Reputation: 2481

Kotlin Android base method not call

I have a base activity like this which has 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 show "onCreate". It means that the onCreate from Base was not called. Can you tell me why the base method is not called? It looks silly but I try and the base was not called The same code works in JAVA

Upvotes: 5

Views: 1545

Answers (1)

Kiskae
Kiskae

Reputation: 25593

You are not overriding the same onCreate method in those two classes. Looking at the documentation it seems one or the other will be called depending on if persistableMode is set to persistAcrossReboots. This means the code in your Base class probably never gets executed regardless of what you do in the subclass.

Upvotes: 6

Related Questions