Search code examples
androidkotlinviewmodel

How to get argument from constructor?


class AlmatyJobViewModel(application: Application) : AndroidViewModel(application){   

 fun clearSuggestionsHistory(){
            SearchRecentSuggestions(application, MySuggestionsProvider.AUTHORITY, MySuggestionsProvider.MODE)
                .clearHistory()
        }
}

Unresolved Referense: application

How to get application from constructor, if i want to use it in this method?


Solution

  • If you add private val application you will have exceptions in compilation time like

    Accidental override: The following declarations have the same JVM signature

    So, to fix it, you only should to call getApplication() where you require.

    SearchRecentSuggestions(getApplication(), MySuggestionsProvider.AUTHORITY, MySuggestionsProvider.MODE).clearHistory()