Does every activity subclass always includes intent property?

CheatActivity.kt
answerIsTrue = intent.getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false)

I don’t see intent variable getting declared anywhere in CheatActivity file. Does it mean it’s always available on every activity subclass? Can someone also point me to documentation showing all default properties/fields of an activity?

Every Activity is started via an Intent, whether that Intent is created by another Activity (QuizActivity in this instance) or by the operating system.

Therefore, the Activity object can reference the Intent that was used to create the Activity. This is done by calling the getIntent() method. The Kotlin syntax allows us to just reference the value we wish to get, here intent, and the compiler converts the request in to the corresponding getter.

For the full Activity documentation, here is the full developer reference: https://developer.android.com/reference/android/app/Activity

1 Like

Yes, Because of Kotlin’s feature of getters and setters,
Using intent property works well.
But I think the following code is easier to understand,
because many people may be wondering “What is intent?”.

answerIsTrue = intent.getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false)
-> answerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false)

In addition to I couldn’t find intent property from anywhere including Kotlin API Docs.
And there’s no explanation about intent property in the book.

Also on the page 126(Printing version),


  • Listing 6.11 Using an extra (CheatActivity.kt)
  • class CheatActivity : AppCompatActivity() {
  • private var answerIsTrue = false
    
  • override fun onCreate(savedInstanceState: Bundle?) {
    
  •     super.onCreate(savedInstanceState)
    
  •     setContentView(R.layout.activity_cheat)
    
  •     answerIsTrue = intent.getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false)
    
  • }
    
  • ...
    
  • }
  • Note that Activity.getIntent() always returns the Intent that started the activity.

Only getIntent() is mentioned, not intent property.

Thank you for your nice book.

The intent property is a Kotlin shorthand for the getIntent() method which allows you to omit the get and () from the access call.