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

[Android] actionViewIntent - activity won't start on Android 11+ #78

Closed
Aure77 opened this issue Nov 10, 2021 · 4 comments · Fixed by #81
Closed

[Android] actionViewIntent - activity won't start on Android 11+ #78

Aure77 opened this issue Nov 10, 2021 · 4 comments · Fixed by #81

Comments

@Aure77
Copy link

Aure77 commented Nov 10, 2021

If your app target Android API 30+ (and use Android 11 device),
using ReactNativeBlobUtil.android.actionViewIntent(path, "application/pdf"); may do nothing.

After investing code, startActivity is not called because resolveActivity always return null here:

if (intent.resolveActivity(pm) != null) {
this.getReactApplicationContext().startActivity(intent);
}

This is due to new Android 11 package visibility.
I found a very interesting article that recommend do no not use resolveActivity - startActivity pattern to avoid potential issues.
See: https://cketti.de/2020/09/03/avoid-intent-resolveactivity/

Why not using try catch method for all Android version like here:

try {
this.getReactApplicationContext().startActivity(intent);
promise.resolve(true);
} catch (ActivityNotFoundException ex) {
promise.reject("ENOAPP", "No app installed for " + mime);
}

and that handle promise resolve/reject correctly.
(this workaround fix my issue with Android 11+ devices)

Full example to reproduce:

// url = url to a pdf file
const PDF_PATH = `${ReactNativeBlobUtil.fs.dirs.CacheDir}/pdfs/Document.pdf`;
const res = await ReactNativeBlobUtil.config({
  fileCache: true,
  appendExt: "pdf",
  path: PDF_PATH
}).fetch("GET", url);
if (res) {
  ReactNativeBlobUtil.android.actionViewIntent(
    res.path(),
    "application/pdf"
  );
}

Further more, actionViewIntent promise is only resolved for Android SDK < 24


Is this normal ?

@Aure77
Copy link
Author

Aure77 commented Nov 10, 2021

I created a patch if someone want to test fix:
https://gist.github.com/Aure77/5b0f75aee85fa897d6a3469146a197df

I can create a PR if confirmed.

@rutgervanwilligen
Copy link

Successfully applied and tested this patch in my project. Using actionViewIntent with a PDF file.

@rutgervanwilligen
Copy link

rutgervanwilligen commented Nov 13, 2021

However, using the solution described in this closed issue also fixes it for me. Not sure which of these is the way to go.

@RonRadtke
Copy link
Owner

RonRadtke commented Nov 14, 2021

@Aure77 would you be so kind to create a PR, I would merge it and create a new update for the lib then.

It's the better way to go. Adjusting the manifest is just a fix for some basically wrong behavior of the lib.

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

Successfully merging a pull request may close this issue.

3 participants