React-Native Android ReactContext returning null -
as react-native appstate
api ios i'm writing appstate event emitter android side of app. in app's mainactivity
on java side, want emit onresume
, onpause
functions tell js side on bridge app either in foreground or background
at moment, can minimise app (and go home-screen on device) , background event emitted correctly. however, when resume app nothing fired...also nothing fired when app opens up.
i've narrowed down fact in these cases mreactinstancemanager.getcurrentreactcontext()
null...for reason.
here's code mainactivity.java
:
@override protected void onpause() { super.onpause(); if (mreactinstancemanager != null) { mreactinstancemanager.onpause(); //send appstate js log.d("react_state", "paused"); //this fires if(mreactinstancemanager.getcurrentreactcontext()!=null) { writablemap params = arguments.createmap(); params.putstring("currentappstate", "background"); sendevent(mreactinstancemanager.getcurrentreactcontext(), "appstatechange", params); } } }
and,
@override protected void onresume() { super.onresume(); if (mreactinstancemanager != null) { mreactinstancemanager.onresume(this, this); //send appstate js log.d("react_state", "resumed"); //this fires if(mreactinstancemanager.getcurrentreactcontext()!=null) { writablemap params = arguments.createmap(); params.putstring("currentappstate", "foreground"); sendevent(mreactinstancemanager.getcurrentreactcontext(), "appstatechange", params); } } }
and emitter code:
private void sendevent(reactcontext reactcontext, string eventname, @nullable writablemap params) { reactcontext .getjsmodule(deviceeventmanagermodule.rctdeviceeventemitter.class) .emit(eventname, params); }
the js side of things set ok , working - can provide code if helps. i'm issue lies on side. trying emit @ wrong time? need move elsewhere? advice appreciated!
ok, i've managed sort this. created new plugin package appstateandroid
emits js. added @ right time in app lifecycle , works perfectly. i'm going post code github (http://github.com/scgough) that's wondering here go:
public class appstateandroidplugin extends reactcontextbasejavamodule implements application.activitylifecyclecallbacks { private static final string plugin_name = "appstateandroid"; private reactcontext mreactcontext; protected activity activity = null; protected activity getactivity(){ return this.activity; } public appstateandroidplugin(reactapplicationcontext reactcontext, activity activity) { super(reactcontext); this.mreactcontext = reactcontext; this.activity = activity; this.activity.getapplication().registeractivitylifecyclecallbacks(this); } private void sendevent(reactcontext reactcontext, string eventname, @nullable writablemap params) { log.d(plugin_name, "sending event"+params.tostring()); reactcontext .getjsmodule(deviceeventmanagermodule.rctdeviceeventemitter.class) .emit(eventname, params); } ... @override public void onactivityresumed(activity activity) { log.d(plugin_name, "resumed"); if(mreactcontext!=null) { writablemap params = arguments.createmap(); params.putstring("currentappstate", "active"); sendevent(mreactcontext, "appstatechange", params); } } @override public void onactivitypaused(activity activity) { log.d(plugin_name, "paused"); if(mreactcontext!=null) { writablemap params = arguments.createmap(); params.putstring("currentappstate", "background"); sendevent(mreactcontext, "appstatechange", params); } } ... @override public void onactivitydestroyed(activity activity) { activity myactivity = this.getactivity(); if (activity == myactivity){ myactivity.getapplication().unregisteractivitylifecyclecallbacks(this); } } @override public string getname() { return plugin_name; } }
you'll need package file in main activity:
mreactinstancemanager = reactinstancemanager.builder() .setapplication(getapplication()) .setbundleassetname("index.android.bundle") .setjsmainmodulename("index.android") .addpackage(new mainreactpackage()) .addpackage(new appstateandroidpluginpackage(this)) ...
Comments
Post a Comment