Today I switched to the new ResultAPI
and I faced this error:
java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
at androidx.fragment.app.FragmentActivity.checkForValidRequestCode(FragmentActivity.java:715)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:673)
at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234)
at androidx.activity.ComponentActivity$2.onLaunch(ComponentActivity.java:207)
at androidx.activity.result.ActivityResultRegistry$3.launch(ActivityResultRegistry.java:147)
at androidx.activity.result.ActivityResultLauncher.launch(ActivityResultLauncher.java:42)
at .MainActivity.getVideo(MainActivity.kt:61)
at .MainActivity.access$getVideo(MainActivity.kt:18)
at .MainActivity$onCreate$2.onClick(MainActivity.kt:42)
at android.view.View.performClick(View.java:5232)
at android.view.View$PerformClick.run(View.java:21289)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
When executing these lines:
private val takeFile = registerForActivityResult(GetContent()) {
Log.e("MainActivity", "fileName: $it")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
btn_get_video.setOnClickListener {
getVideo()
}
}
private fun getVideo() {
takeFile.launch("video/*")
}
There is no way to declare REQUEST_CODE
manually with this new result API.
NOTICE:
It sometimes works!
[UPDATE]
I registered multiple contracts (TakeVideo & GetContent) and I found that the order of registration is important (the first one works like a charm but the others will crash). maybe we have to register just a single contract for each activity?!
[UPDATE #2]
The problem has been fixed in the latest version of activity (1.2.7-alpha07). now it's warning you about adding both activity and fragment related dependencies.