-
Notifications
You must be signed in to change notification settings - Fork 24.5k
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
ReactContext: currentActivity is null when onActivityResult is called #8694
Comments
Can you provide some code? |
Yes, but it's in Kotlin, do you mind? |
I'm not familiar with Kotlin, but for other people who will check this that will be better than nothing I guess. |
Stripped version: package at.feibra.mobileqm
class ControllerModule(ctx: ReactApplicationContext) : ReactContextBaseJavaModule(ctx) {
// ~ Constructors ----------------------------------------------------------------------------------
init {
/* forward activity event to controller */
ctx.addActivityEventListener { requestCode, resultCode, data ->
L.info("activity result: requestCode: {}, resultCode: {}, data: {}", requestCode, resultCode, data);
}
/* forward resume, pause, destroy to controller */
ctx.addLifecycleEventListener(object : LifecycleEventListener {
override fun onHostResume() {
L.info("host resumed")
}
override fun onHostPause() {
L.info("host paused")
}
override fun onHostDestroy() {
}
})
}
} |
I can confirm this that I've run into this issue. cc @foghina |
Yeah, this is how Android works, unfortunately. If you have a plain @Override
public void onHostResume() {
if (mActivityResult != null) {
// getCurrentActivity() will never return null here
handleActivityResult(getCurrentActivity(), mActivityResult);
mActivityResult = null;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
final Activity activity = getCurrentActivity();
if (activity != null) {
handleActivityResult(activity, data);
} else {
mActivityResult = data;
}
}
private void handleActivityResult(Activity activity, Intent result) {
// insert business logic
} I'd write this in Kotlin but I'm pretty rusty 🙃 |
I've worked around it like that already. I still think this needs fixing: Usually, without RN, people would override Please reopen. |
@foghina Please reopen this issue. You said RN wants to mimic Android as much as possible. In a regular Android application you have access to your Activity on |
Fine. Feel free to send a PR that e.g. adds an |
@foghina Awesome thanks. I can do that, but I noticed another problem which might be related. I worked around like you said by storing the result and waiting for the Activity to return. Now I tested my app for low-memory using "Don't keep activities". When using that |
Ah yes, the problem there is the |
@foghina Ok. I would help, but I don't know what the "structural plan" is. One Is the actual JS structure (the component-tree, the callbacks) tied to the |
Has this be changed with 0.31? Thanks! |
Any idea when this will be fixed? |
This should be fixed by 3c4fd4. However, note that if the activity returning a result is also a RN activity, it will be returned by If the activity returning the result is not RN (e.g. camera app), current activity will be correct. Also, running with don't keep activities is probably still broken. Working on a fix for these two issues. |
Summary: The Android lifecycle is weird: turns out `onActivityResult` is called before `onResume`. This means `getCurrentActivity()` could return the wrong instance, or `null` if the activity was destroyed. To give developers access to the Activity receiving the result (which is also about to become the current activity), pass it as an argumento the listener. Fixes github issue #8694. Reviewed By: donyu Differential Revision: D3704141 fbshipit-source-id: e7e00ccc28114f97415e5beab8c9b10cb1e530be
@foghina So does this work now when not keeping activities? If not, should I open a separate bug-report? |
It should work, I've tested a bunch of scenarios but not |
@foghina I'll test it when it's released (in 0.32?) |
Probably |
0.33-rc.0 next Monday unless this is critical to get as a patch into 0.32.0. |
@bestander It is not critical to me, but, since Android is allowed to kill Activities any time, this can lead to curious bugs for inexperienced developers, which in turn creates new issues for this already overwhelmed project… |
@fab1an, 0.32 becomes stable early next week and 0.33-rc is released. |
Summary: The Android lifecycle is weird: turns out `onActivityResult` is called before `onResume`. This means `getCurrentActivity()` could return the wrong instance, or `null` if the activity was destroyed. To give developers access to the Activity receiving the result (which is also about to become the current activity), pass it as an argumento the listener. Fixes github issue facebook#8694. Reviewed By: donyu Differential Revision: D3704141 fbshipit-source-id: e7e00ccc28114f97415e5beab8c9b10cb1e530be
This is actually a big deal. This patch seems to fix a whole class of issues for us. We will be testing .33-rc over the weekend. |
@conover Did it fix your issues? |
I can confirm this fixed as of version 0.34, I did not try 0.33. |
I hava the same issues. android version 0.36 |
I use
addActivityEventListener
to listen for the result of an Intent.onHostPause
and releases the referencemCurrentActivity
inReactContext.java
.onActivityResult
fires, but there is nocurrentActivity
set yet, becauseonHostResume
is called afterwards.0.29 / Android / Mac
The text was updated successfully, but these errors were encountered: