register while current state is RESUMED. LifecycleOwners must call register before they are STARTED
意思就是registerForActivityResult必須在生命周期STARTED之前調(diào)用
解決:將registerForActivityResult的創(chuàng)建移動(dòng)到onCreate()中去
private ActivityResultLauncher<Intent> intentActivityResultLauncher;
在onCreate()寫:
intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result->{
//此處是跳轉(zhuǎn)的result回調(diào)方法
Log.d(TAG,result.getData().getStringExtra("dat"));
if (result.getData() != null && result.getResultCode() == Activity.RESULT_OK) {
}
});
需要跳轉(zhuǎn)頁面的時(shí)候:
private void createVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//使用0,錄制1分鐘大概內(nèi)存是幾兆
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
//當(dāng)在這里設(shè)置時(shí)長(zhǎng)之后,錄制到達(dá)時(shí)間狱掂,系統(tǒng)會(huì)自動(dòng)保存視頻温技,停止錄制
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 61);
// 限制大小 限制視頻的大小,這里是100兆。當(dāng)大小到達(dá)的時(shí)候,系統(tǒng)會(huì)自動(dòng)停止錄制
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 1024 * 1024 * 100);
//在這里有錄制完成之后的操作,系統(tǒng)會(huì)默認(rèn)把視頻放到照片的文件夾中
//startActivityForResult(intent, 11);
intentActivityResultLauncher.launch(intent);
}