1

When I put the following code in the Android Studio, Android Studio does not prompt me to import observe from androidx.lifecycle. I have to manually do the import and I am starting to get tired of it.

        startFragmentViewModel.suggestions.observe(this) {
            it?.let { searchView.addSuggestions(it) }
        }

Supposing we write a function like so:

//fun Fragment.importObserve() {
//    import androidx.lifecycle.observe
//}

Is there a way we can do something like that?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Gilbert
  • 2,699
  • 28
  • 29
  • This question answers your question quite well https://stackoverflow.com/questions/24406225/auto-import-not-working-for-android-classes-in-android-studio – Dango3 May 20 '20 at 14:02
  • @Dango3 I checked out the link, it doesn't answer my question. – Gilbert May 20 '20 at 18:50

1 Answers1

1

Due to the issue reported here, this is actually normal behavior, as when moving to Kotlin 1.4 or above, androidx.lifecycle.observe will be deprecated, and you can use in-build observe extension function instead.

The opposed behavior (auto adding the import) is actually not recommended.

Samuel T. Chou
  • 521
  • 6
  • 31