简体   繁体   中英

Where can I put Kotlin code I want to run on app startup when using flutter?

I have some initialisation kotlin code I want to run when my flutter app starts.

My app already has a MainActivity.kt that exists in app/src/main/kotlin/com/example/my_app/

I then added an onCreate to this activity and added an exception to confirm this code executed....but it did not...

class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)
        throw RuntimeException("blah");
    }
}

Where can I put kotlin initialization code that I want to execute on application launch?

You use a different oncreate method android first calls this method while loading android app it is lifecycle method.

class MainActivity : FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

      
        throw RuntimeException("blah");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM