SlideShare a Scribd company logo
1 of 32
Download to read offline
The recent movement in
Activity and Fragment
Wonyoung Choi
@TORU_0239
I am…
Wonyoung Choi, a.k.a Toru
Android Advocate,
Singapore Tourism Board
@TORU_0239
Github: TORU239
LinkedIn: linkedin.com/in/toruchoi
The latest changes
The latest changes
• Noteworthy changes in the latest alpha version
• Activity Results API in activity 1.2.0 alpha
• Fragment Result API in fragment 1.3.0 alpha
startActivityForResult()
onActivityResult()
onRequestPermissionsResult()
startActivityForResult()
onActivityResult()
onRequestPermissionsResult()
Deprecated on ComponentActivity
Activity Results API
• Deprecated method we have been familiar with.
• ActivityResultLauncher and ActivityResultContract
• Resolution for Tight Coupling, Type-Safety, Unwanted NPE
implementation 'androidx.activity:activity-ktx:1.2.0-alpha04'
import androidx.activity.result.contract.ActivityResultContracts.*
val requestActivity = registerForActivityResult(StartActivityForResult()){ activityResult ->
// Do what you want
}
binding.btn.setOnClickListener {
requestActivity(Intent(...))
}
import androidx.activity.result.contract.ActivityResultContracts.*
val requestActivity = registerForActivityResult(StartActivityForResult()){ activityResult ->
// Do what you want
}
binding.btn.setOnClickListener {
requestActivity(Intent(...))
}
ActivityResultLauncher ActivityResultContract
Activity Results API
• Available to get result returned like typical Intent
• e.g. Picking Image, Opening Camera using Intent
val pickImages = registerForActivityResult(GetContent()){ uri ->
...
}
binding.btnImagePick.setOnClickListener {
pickImages.launch("image/*")
}
Activity Results API
• Can handle permission dialog and callback
• Not only single permission but multiple one
val permission = registerForActivityResult(RequestPermission()){ enabled ->
// Do what you want
}
binding.btnPermission.setOnClickListener {
permission.launch(android.Manifest.permission.ACCESS_FINE_LOCATION)
}
val multiplePermission = registerForActivityResult(RequestMultiplePermissions()) { map ->
// Can do actions we want
}
binding.btnMultipermission.setOnClickListener {
multiplePermission(
arrayOf(
android.Manifest.permission.CAMERA,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
)
)
}
CreateDocument()
GetContent()
GetMultipleContents()
OpenDocument()
OpenDocumentTree()
OpenMultipleDocuments()
PickContact()
RequestMultiplePermissions()
RequestPermission()
StartActivityForResult()
StartIntentSenderForResult()
TakePicture()
TakePicturePreview()
TakeVideo()
Activity Results API
Activity Results API
• Customisable ActivityResultContract
• When users need to implement features upon their purpose
val customContract = object:ActivityResultContract<String, ActivityResult>(){
override fun createIntent(
context: Context,
input: String?
): Intent {
return Intent(this@CurrentActivity, ResultActivity::class.java).putExtra("test", input)
}
override fun parseResult(
resultCode: Int,
intent: Intent?
): ActivityResult {
return ActivityResult(resultCode, intent)
}
}
val customContract = object:ActivityResultContract<String, ActivityResult>(){
override fun createIntent(
context: Context,
input: String?
): Intent {
return Intent(this@CurrentActivity, ResultActivity::class.java).putExtra("test", input)
}
override fun parseResult(
resultCode: Int,
intent: Intent?
): ActivityResult {
return ActivityResult(resultCode, intent)
}
}
val customActivity = registerForActivityResult(customContract){ activityResult->
// Can do whatever
}
Fragment Results API
• To pass data between Fragments
• No need sharedViewModel or Listener in Activity
• FragmentManager in responsible for working on this
Fragment Results API
implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha04'
setFragmentResult("requestKey", bundleOf("key" to "value"))
fun Fragment.setFragmentResult(
requestKey: String,
result: Bundle?
) = parentFragmentManager.setFragmentResult(requestKey, result)
Sender Fragment
setFragmentResultListener("requestKey"){ resultKey, bundle ->
// Do whatever you want
}
fun Fragment.setFragmentResultListener(
requestKey: String,
listener: ((resultKey: String, bundle: Bundle) -> Unit)?
) {
parentFragmentManager.setFragmentResultListener(requestKey, this, listener)
}
Receiver Fragment
Fragment Results API
setFragmentResult("NESTED_REQUEST_KEY", bundleOf("key" to "value"))
fun Fragment.setFragmentResult(
requestKey: String,
result: Bundle?
) = parentFragmentManager.setFragmentResult(requestKey, result)
Child Fragment
childFragmentManager
.setFragmentResultListener(
"NESTED_REQUEST_KEY",
this,
FragmentResultListener { requestKey, result ->
// Whatever you want
}
)
Parent Fragment
Further Discussion
Limitations
•Still stays at alpha stage
•Possibilities of unexpected changes
Further Discussion
•Mechanism of API
•Receiving an activity result in a separate class
•Utilising and combine all the new components
Thank You!
Wonyoung Choi
@TORU_0239

More Related Content

What's hot

CompletableFuture
CompletableFutureCompletableFuture
CompletableFuture
koji lin
 
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ
Swift で数学のススメ 〜 プログラミングと数学は同時に学べSwift で数学のススメ 〜 プログラミングと数学は同時に学べ
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ
Taketo Sano
 

What's hot (20)

Mobx Performance and Sanity
Mobx Performance and SanityMobx Performance and Sanity
Mobx Performance and Sanity
 
JavaOne 2013: Java 8 - The Good Parts
JavaOne 2013: Java 8 - The Good PartsJavaOne 2013: Java 8 - The Good Parts
JavaOne 2013: Java 8 - The Good Parts
 
The redux saga begins
The redux saga beginsThe redux saga begins
The redux saga begins
 
The evolution of redux action creators
The evolution of redux action creatorsThe evolution of redux action creators
The evolution of redux action creators
 
Functional Vaadin talk at OSCON 2014
Functional Vaadin talk at OSCON 2014Functional Vaadin talk at OSCON 2014
Functional Vaadin talk at OSCON 2014
 
CompletableFuture
CompletableFutureCompletableFuture
CompletableFuture
 
Higher Order Components and Render Props
Higher Order Components and Render PropsHigher Order Components and Render Props
Higher Order Components and Render Props
 
Martin Anderson - threads v actors
Martin Anderson - threads v actorsMartin Anderson - threads v actors
Martin Anderson - threads v actors
 
Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6Redux saga: managing your side effects. Also: generators in es6
Redux saga: managing your side effects. Also: generators in es6
 
Using Redux-Saga for Handling Side Effects
Using Redux-Saga for Handling Side EffectsUsing Redux-Saga for Handling Side Effects
Using Redux-Saga for Handling Side Effects
 
Fullstack Conference - Proxies before proxies: The hidden gems of Javascript...
Fullstack Conference -  Proxies before proxies: The hidden gems of Javascript...Fullstack Conference -  Proxies before proxies: The hidden gems of Javascript...
Fullstack Conference - Proxies before proxies: The hidden gems of Javascript...
 
Angular mix chrisnoring
Angular mix chrisnoringAngular mix chrisnoring
Angular mix chrisnoring
 
Angular - injection tokens & Custom libraries
Angular - injection tokens & Custom librariesAngular - injection tokens & Custom libraries
Angular - injection tokens & Custom libraries
 
React lecture
React lectureReact lecture
React lecture
 
Architecture for scalable Angular applications (with introduction and extende...
Architecture for scalable Angular applications (with introduction and extende...Architecture for scalable Angular applications (with introduction and extende...
Architecture for scalable Angular applications (with introduction and extende...
 
JS Fest 2019. Anjana Vakil. Serverless Bebop
JS Fest 2019. Anjana Vakil. Serverless BebopJS Fest 2019. Anjana Vakil. Serverless Bebop
JS Fest 2019. Anjana Vakil. Serverless Bebop
 
Functional Reactive Endpoints using Spring 5
Functional Reactive Endpoints using Spring 5Functional Reactive Endpoints using Spring 5
Functional Reactive Endpoints using Spring 5
 
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ
Swift で数学のススメ 〜 プログラミングと数学は同時に学べSwift で数学のススメ 〜 プログラミングと数学は同時に学べ
Swift で数学のススメ 〜 プログラミングと数学は同時に学べ
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 
視覚化とSwiftのタイプについて
視覚化とSwiftのタイプについて視覚化とSwiftのタイプについて
視覚化とSwiftのタイプについて
 

Similar to activity_and_fragment_may_2020_lakopi

Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
Woody Pewitt
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
ShriKant Vashishtha
 

Similar to activity_and_fragment_may_2020_lakopi (20)

What's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritageWhat's in Kotlin for us - Alexandre Greschon, MyHeritage
What's in Kotlin for us - Alexandre Greschon, MyHeritage
 
Durable functions 2.0 (2019-10-10)
Durable functions 2.0 (2019-10-10)Durable functions 2.0 (2019-10-10)
Durable functions 2.0 (2019-10-10)
 
Is java8a truefunctionallanguage
Is java8a truefunctionallanguageIs java8a truefunctionallanguage
Is java8a truefunctionallanguage
 
Is java8 a true functional programming language
Is java8 a true functional programming languageIs java8 a true functional programming language
Is java8 a true functional programming language
 
Azure Durable Functions (2019-04-27)
Azure Durable Functions (2019-04-27)Azure Durable Functions (2019-04-27)
Azure Durable Functions (2019-04-27)
 
Qt Workshop
Qt WorkshopQt Workshop
Qt Workshop
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 
Exploring Kotlin
Exploring KotlinExploring Kotlin
Exploring Kotlin
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in Golang
 
Ngrx meta reducers
Ngrx meta reducersNgrx meta reducers
Ngrx meta reducers
 
Azure Durable Functions (2019-03-30)
Azure Durable Functions (2019-03-30) Azure Durable Functions (2019-03-30)
Azure Durable Functions (2019-03-30)
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
Android best practices
Android best practicesAndroid best practices
Android best practices
 
React & The Art of Managing Complexity
React &  The Art of Managing ComplexityReact &  The Art of Managing Complexity
React & The Art of Managing Complexity
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
 
Aplicações Assíncronas no Android com Coroutines e Jetpack
Aplicações Assíncronas no Android com Coroutines e JetpackAplicações Assíncronas no Android com Coroutines e Jetpack
Aplicações Assíncronas no Android com Coroutines e Jetpack
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 
Asynchronous Programming at Netflix
Asynchronous Programming at NetflixAsynchronous Programming at Netflix
Asynchronous Programming at Netflix
 
Reactive programming every day
Reactive programming every dayReactive programming every day
Reactive programming every day
 
react-hooks.pdf
react-hooks.pdfreact-hooks.pdf
react-hooks.pdf
 

More from Toru Wonyoung Choi

More from Toru Wonyoung Choi (10)

Glancing essential features of Dart, before stepping into Flutter
Glancing essential features of Dart, before stepping into FlutterGlancing essential features of Dart, before stepping into Flutter
Glancing essential features of Dart, before stepping into Flutter
 
The use case of a scalable architecture
The use case of a scalable architectureThe use case of a scalable architecture
The use case of a scalable architecture
 
Jetpack, with new features in 2021 GDG Georgetown IO Extended
Jetpack, with new features in 2021 GDG Georgetown IO ExtendedJetpack, with new features in 2021 GDG Georgetown IO Extended
Jetpack, with new features in 2021 GDG Georgetown IO Extended
 
Building an app with Google's new suites
Building an app with Google's new suitesBuilding an app with Google's new suites
Building an app with Google's new suites
 
datastore_devfest2020_incheon
datastore_devfest2020_incheondatastore_devfest2020_incheon
datastore_devfest2020_incheon
 
Slide_Concat_adapter_july_2020
Slide_Concat_adapter_july_2020Slide_Concat_adapter_july_2020
Slide_Concat_adapter_july_2020
 
camera_x_beyond_alpha
camera_x_beyond_alphacamera_x_beyond_alpha
camera_x_beyond_alpha
 
Slide_For_GDGKL_devfest_2019
Slide_For_GDGKL_devfest_2019Slide_For_GDGKL_devfest_2019
Slide_For_GDGKL_devfest_2019
 
CameraX, MLKit and AutoML at DevFest Songdo 2019
CameraX, MLKit and AutoML at DevFest Songdo 2019CameraX, MLKit and AutoML at DevFest Songdo 2019
CameraX, MLKit and AutoML at DevFest Songdo 2019
 
CameraX, MLKit and AutoML at DevFest Cebu 2019
CameraX, MLKit and AutoML at DevFest Cebu 2019CameraX, MLKit and AutoML at DevFest Cebu 2019
CameraX, MLKit and AutoML at DevFest Cebu 2019
 

Recently uploaded

Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Cara Menggugurkan Kandungan 087776558899
 

Recently uploaded (7)

Mobile App Penetration Testing Bsides312
Mobile App Penetration Testing Bsides312Mobile App Penetration Testing Bsides312
Mobile App Penetration Testing Bsides312
 
Android Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesAndroid Application Components with Implementation & Examples
Android Application Components with Implementation & Examples
 
Mobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsMobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s Tools
 
Mobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsMobile Application Development-Components and Layouts
Mobile Application Development-Components and Layouts
 
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 
Bromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pure
Bromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pureBromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pure
Bromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pure
 
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
 

activity_and_fragment_may_2020_lakopi