registerForActivityResult()
时间: 2024-05-04 21:08:01 浏览: 15
registerForActivityResult() is a method in the Android Jetpack library that allows an Activity or Fragment to register for a specific result from another Activity or Fragment. It replaces the traditional startActivityForResult() method, which is now considered deprecated in the Android API.
The registerForActivityResult() method takes two parameters: a contract and a callback. The contract defines the type of activity or fragment result to be returned, such as picking an image or selecting a contact. The callback is invoked when the result is returned, passing in the result code and any data associated with the result.
Using registerForActivityResult() simplifies the process of handling activity or fragment results, as it eliminates the need to override the onActivityResult() method and manage result codes and data manually. It also provides type safety and ensures that the result is returned to the correct activity or fragment.