Search code examples
androidkotlinfragmentoncreateoptionsmenu

Android onCreateOptionsMenu declaration in a fragment failing with "Accidental Override"


I have some Kotlin code that is attempting to tweak the visibility of some option menu items inside a fragment using onCreateMenuOptions. Kotlin is complaining about an accidential override, and there being dual onCreateMenuOption declarations in the code.

Nothing I am doing explicitly is doing this, so I'm guessing this is something Kotlin is generating in the background, akin to the getters created on object constructors, but I'm at a loss what is triggering this, nor how to fix it. An onCreateOptionsMenu declaration from within an activity works fine (but does have a different signature). I just tried dropping an onCreateOptionsMenu declaration into another independent fragment, and got the same result.

Any clues or ideas what is causing this, or how to determine exactly what Kotlin is generating behind the scenes?

Thanks and regards - Andrew

  • as a followup just tried onPrepareOptionsMenu in the same context, and this works fine. So I'm pretty sure I can work around my immediate problem using onPrepare, but still curious what is happening with the onCreate

Solution

  • https://developer.android.com/codelabs/android-navigation#8

    you use jetpack navigation here is link where you know how to use it,it is much better than other