Last updated on 12/21/23
Test Your Knowledge
- Optimize your Android app
What is the purpose of the second parameter (
requestCode) in the
startActivityForResultmethod (Intent, int)?
It prevents problems with other applications using the same code.
It links up with the requestCode of the Activity in Manifest.
It identifies the request and helps the receiving Activity determine which result corresponds to which request.
It’s the Intent security key—it checks that it hasn’t been infected.
You want to launch a new Activity with
startActivityForResult(). Which method should you override in the calling Activity to retrieve a result when the second Activity closes?
onCreate()method, you retrieve the result with the
onActivityResult()method, you retrieve the information from the Intent passed as a parameter.
You retrieve the data using the
getIntent().getExtras()method in any method of the life cycle.
onSavedInstanceState()method, the Bundle passed as a parameter allows you to retrieve the information of the closed Activity.
Which “key-value” type data persistence tool can you use so the user does not have to re-enter their name every time the app launches?
You can use a relational database to store the name as a String.
You can use the Intent of the Activity: it saves the name in between app launches.
You can use
SharedPreferencesto persist a String: it can be stored and retrieved with a constant key.
You can use a static variable, which persists the name in between app launches.