Main Activity
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
Hilt Module
@Module
@InstallIn(SingletonComponent::class)
class AppModule {
@Singleton
@Provides
fun provideActivity(): MainActivity = MainActivity()
@Singleton
@Provides
fun provideActivityUtils(@ActivityRetainedScoped context: MainActivity): ActivityUtils =
ActivityUtilsImpl(context)
}
And utils class with needed functions
class ActivityUtilsImpl @Inject constructor(@ActivityRetainedScoped private val activity:
MainActivity) : ActivityUtils {
override fun showKeyboard() {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as
InputMethodManager?
imm?.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0)
}
}
When I try to call something from the utils class using an activity instance, I get the error java.lang.IllegalStateException: System services not available to Activities before onCreate ()
Tried @ActivityContext the same thing, how can I make it call normally
Source: Android Questions