react-native: [Android][0.29] No longer possible to access Activity from SimpleViewManager
According to the 0.29.0 upgrade docs, it’s now very easy to extend ReactContextBaseJavaModule
and use getCurrentActivity
to get the activity reference in native Android modules. However, it no longer appears to be possible to get access to the current activity from native Android UI Components.
Continuing a discussion started here, I need access to the activity so that I can create a FragmentManager
to add a Fragment
to the FrameLayout
being vended from SimpleViewManager
. There was a suggestion that ReactContext.getCurrentActivity()
be made public, which seems like a bad idea, but I can’t think of another way.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 8
- Comments: 18 (6 by maintainers)
Commits related to this issue
- Add ReactInstanceHolder Reviewed By: astreet Differential Revision: D3328342 fbshipit-source-id: af4e825d0b7c2d3d4490094a939e97cc527dd242 — committed to facebook/react-native by foghina 8 years ago
- Make getCurrentActivity public on ReactContext Summary: Addresses #8661 Closes https://github.com/facebook/react-native/pull/9071 Differential Revision: D3641285 Pulled By: foghina fbshipit-source... — committed to facebook/react-native by deleted user 8 years ago
- Make getCurrentActivity public on ReactContext Summary: Addresses #8661 Closes https://github.com/facebook/react-native/pull/9071 Differential Revision: D3641285 Pulled By: foghina fbshipit-source... — committed to facebook/react-native by deleted user 8 years ago
- Make getCurrentActivity public on ReactContext Summary: Addresses #8661 Closes https://github.com/facebook/react-native/pull/9071 Differential Revision: D3641285 Pulled By: foghina fbshipit-source... — committed to immidi/react-native by deleted user 8 years ago
- Make getCurrentActivity public on ReactContext Summary: Addresses #8661 Closes https://github.com/facebook/react-native/pull/9071 Differential Revision: D3641285 Pulled By: foghina fbshipit-source... — committed to iodine/react-native by deleted user 8 years ago
- Make getCurrentActivity public on ReactContext Summary: Addresses #8661 Closes https://github.com/facebook/react-native/pull/9071 Differential Revision: D3641285 Pulled By: foghina fbshipit-source... — committed to HomePass/react-native by deleted user 8 years ago
Having
getPackages
take anActivity
parameter is a much worse idea than makingReactContext#getCurrentActivity()
public, for the following reasons:ReactContext
/ react instance) creation to anActivity
, as we want to be able to initialize a react instance in any context.getCurrentActivity()
suggests, theActivity
can change over time. Holding a reference to whatever the activity was at react instance creation time is both incorrect and a memory leak.As long as we properly document
getCurrentActivity()
(I think it already is documented in caps that you shouldn’t hold references to the result), it should be fine to make it public.ThemedReactContext#getCurrentActivity()
seems to be allwaysnull
in react-native version 0.31.0. At least on property setting it is stillnull
. @marcshilling on which react-native version did your workaround worked?I’m not sure. What I’m doing is creating a placeholder object:
And then actually doing the work of adding the activity-dependent view to the FrameLayout later on when props change.
@foghina any thoughts on this? Seems like something super useful to get fixed.
@nickjanssen that’s actually different from the issue being discussed here. You are extending
ReactContextBaseJavaModule
, which has access togetCurrentActivity()
. See this comment about why you can’t usegetCurrentActivity()
in the constructor.This issue is regarding
SimpleViewManager
extensions not havinggetCurrentActivity()
, which is an issue that still remains in0.30.0
.+1 I also cannot access Activity any longer, I need it in the constructor phase in order to get a SplashScreen working. Calling
getCurrentActivity()
in the constructor simply returns null.It didn’t.