Skip to content

Hilt ‐ Field Injection

Devrath edited this page Oct 8, 2023 · 3 revisions

Observation

  • In Hilt there is no component so either we use Inject annotation for the implementation or use the module to create an object of the class.
  • In our example, we have not used a module since we are able to use @Inject on the constructor of the implementation. The hilt takes care of creating the object.
  • Now @AndroidEntryPoint forms the starting part of instance creation. If you have used dagger we know that we need to build the component explicitly.
  • In the output you can see when the class got loaded, all the instances Macintosh, Microsoft, and Computer are created.
  • When you click the button observe the address, If you click again it will use the same address since the object uses the same address.
  • Now if you rotate the device, This would result in the destruction and recreation of the activity and results on creating the instances again and if you do not have the address, the address is brand new.

Output

// ----------> Class is loaded for the first time 
Macintosh constructor is invoked !
Microsoft constructor is invoked !
Computer constructor is invoked !
// ----------> Clicking the button to access the instance to perform a action the first time
Computer instance -> 122403514
Computer has started
// ----------> DEvice is rotated
Macintosh constructor is invoked !
Microsoft constructor is invoked !
Computer constructor is invoked !
// ----------> Clicking the button to access the instance to perform an action the second time
Computer instance -> 46739089
Computer has started
// ----------> Clicking the button to access the instance to perform an action a third time but now without rotation
Computer instance -> 46739089
Computer has started

Code

Implementation

Macintosh.kt

class Macintosh @Inject constructor() {
    init { PrintUtils.printLog("Macintosh constructor is invoked !") }
}

Microsoft.kt

class Microsoft @Inject constructor() {
    init { printLog("Microsoft constructor is invoked !") }
}

Computer.kt

class Computer @Inject constructor( var mac: Macintosh, var windows: Microsoft ) {
    init { printLog("Computer constructor is invoked !") }
    fun start() { printLog("Computer has started") }
}

Activity

MyActivity.kt

@AndroidEntryPoint
class HiltFieldInjectionActivity : AppCompatActivity() {

    private lateinit var binding: ActivityHiltFieldInjectionBinding

    @Inject lateinit var computer: Computer

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityHiltFieldInjectionBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setOnClickListeners();
    }

    private fun setOnClickListeners() {
        binding.apply {
            initiateId.setOnClickListener {
                PrintUtils.printLog("Computer instance -> "+computer.hashCode().toString())
                computer.start()
            }
        }
    }
}
Clone this wiki locally