Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve] Use ActivityResultLauncher instead of onActivityResult, because old way has been deprecated. #283

Open
dphans opened this issue Dec 16, 2020 · 4 comments

Comments

@dphans
Copy link

dphans commented Dec 16, 2020

I know that onActivityResult deprecated but still used in next API versions. But I suggest to upgrade to consistent with newer API.

Old way (Kotlin)

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
   @Suppress("DEPRECATION")
   super.onActivityResult(requestCode, resultCode, data)
   getViewBinding().advancedWebView.onActivityResult(requestCode, resultCode, data)
}

New way (Kotlin)

private val mOnWebViewActivityResult = registerForActivityResult(
   ActivityResultContracts.StartActivityForResult()
) { result ->
   getViewBinding().advancedWebView.handleActivityResultCallback(result)
}
@ocram
Copy link
Contributor

ocram commented Dec 16, 2020

Thanks!

We might, at some point, switch to the new API, but that is absolutely low-priority.

More information on the API is available here as well: https://developer.android.com/training/basics/intents/result

@mhemon
Copy link

mhemon commented Dec 23, 2020

Can you provide new way in java fragment code?

@developerGM
Copy link

@mhemon did you solve using the new way in java?

@developerGM
Copy link

you can change original code with this:

public void setListener(final Fragment fragment, final Listener listener, final int requestCodeFilePicker) {
        if (fragment != null) {
            mFragment = new WeakReference<Fragment>(fragment);
            mSelectFileResultLauncher = mFragment.get().registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
                onActivityResult(requestCodeFilePicker, result.getResultCode(), result.getData());
            });
        } else {
            mFragment = null;
        }
        setListener(listener, requestCodeFilePicker);
}
 if (mFragment != null && mFragment.get() != null && Build.VERSION.SDK_INT >= 11) {
            mSelectFileResultLauncher.launch(i);
//            mFragment.get().startActivityForResult(Intent.createChooser(i, getFileUploadPromptLabel()), mRequestCodeFilePicker);
        } else if (mActivity != null && mActivity.get() != null) {
            mActivity.get().startActivityForResult(Intent.createChooser(i, getFileUploadPromptLabel()), mRequestCodeFilePicker);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants