Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 419
coinhndp Avatar asked Oct 07 '17 14:10

coinhndp


1 Answers

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.

like image 177
Kiskae Avatar answered Sep 18 '22 10:09

Kiskae