android - Update Initial Props on Reload in React Native -
i have react native application (android) returns initial props activity delegate:
public class mainreactactivitydelegate extends reactactivitydelegate { private integer count = 1; ... @android.support.annotation.nullable @override protected bundle getlaunchoptions() { bundle initialprops = new bundle(); initialprops.putstring("count", this.count); return initialprops; } ... }
i increment count
in 1 of components works fine when shake reload getlaunchoptions
not called , initial value of 1
used.
is there hook in can update initial props when reloading app?
you can use reactinstancemanager.addreactinstanceeventlistener
hook reload event , reactrootview.setappproperties
update initial props.
mainactivity.java
public class mainactivitytest extends reactactivity { private reactinstancemanager minstancemanager; private reactactivitydelegate mdelegate; private reactrootview mreactrootview; /** * returns name of main component registered javascript. * used schedule rendering of component. */ @override protected string getmaincomponentname() { return "projectname"; } @override protected reactactivitydelegate createreactactivitydelegate() { //create custom reactactivitydelegate mdelegate = new reactactivitydelegate(this, getmaincomponentname()) { @override protected reactrootview createrootview() { mreactrootview = new reactrootview(mainactivitytest.this); return mreactrootview; } }; return mdelegate; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (minstancemanager == null) { minstancemanager = mdelegate.getreactinstancemanager(); //hook reload event minstancemanager.addreactinstanceeventlistener(new reactinstancemanager.reactinstanceeventlistener() { @override public void onreactcontextinitialized(reactcontext context) { //random num test case int num = (int) (math.random() * 100); bundle initialprops = new bundle(); initialprops.putstring("count", num + ""); //update init props when app reload mreactrootview.setappproperties(initialprops); log.e(reactconstants.tag, "onreactcontextinitialized: " + num); } }); } } }
but restore count
after app reloaded, may not ideally solution.you better create native modules sync count
data between javascript , java.
Comments
Post a Comment