kotlinandroid-studioimport

How to make android studio use class imports for inline classes


I have this interface:

@Immutable
sealed interface TestUiAction{
    data object OnClick
}

When I try to call it in android studio by clicking on auto suggestion:

enter image description here

It imports the class like this:

import package.TestUiAction

fun onUiAction(uiAction: TestUiAction){
    when(uiAction){
        is TestUiAction.OnClick -> TODO()
    }
}

Question: How can I make it import it like this instead?

import package.TestUiAction.OnClick

fun onUiAction(uiAction: TestUiAction){
    when(uiAction){
        is OnClick -> TODO()
    }
}


Solution

  • I don't think this can be done in a single step, but you can always change the import afterwards. Using shortcuts that is very quick:

    As soon as you autocompleted TestUiAction.OnClick just press Alt + Enter and select the quick-fix Add import for package.TestUiAction.OnClick. You can incrementally search the list of quick-fixes by typing, im should be sufficient.

    That's all, the import is changed to import package.TestUiAction.OnClick and you can ue the nested class OnClick directly.

    Just make sure you enable this quick-fix in the settings in Edit -> Intentions by ticking the checkbox for Kotlin's "Add import for member".