Why native WLClient lifecycle bind to Activity lif

2019-08-04 04:46发布

Short Version

I've found that WLClient lifecycle is bound to Activity. Activity lifecycle is managed by OS.

Do you have any recommendations to compensate this?

Long Version

For Android Native project, the first step should be create a WLClient instance by the following call

WLClient.getInstance(Context ctx)

From the API document, it's a Context, not an Activity, but in reality, there would be a type cast in WLPush.java in WL framework.

It doesn't make sense to bind a WLClient to a Activity, whose life cycle is not controlled by us. So for the safety concern, each WLClient should be re-created, when a new Activity is created.

And for each re-creation, the following are needed:

  1. connect to server
  2. construct challenge handler

I have the following error if I passed a class (MainContext) inheriting Application (which is a Context):

09-26 13:33:29.571: E/AndroidRuntime(32300): FATAL EXCEPTION: pool-2-thread-4
09-26 13:33:29.571: E/AndroidRuntime(32300): Process: com.XXXX, PID: 32300
09-26 13:33:29.571: E/AndroidRuntime(32300): java.lang.ClassCastException: com.XXX.MainContext cannot be cast to android.app.Activity
09-26 13:33:29.571: E/AndroidRuntime(32300):    at com.worklight.wlclient.api.WLPush.<init>(WLPush.java:151)
09-26 13:33:29.571: E/AndroidRuntime(32300):    at com.worklight.wlclient.api.WLClient.getPush(WLClient.java:673)
09-26 13:33:29.571: E/AndroidRuntime(32300):    at com.worklight.wlclient.WLRequest.requestFinished(WLRequest.java:203)
09-26 13:33:29.571: E/AndroidRuntime(32300):    at com.worklight.wlclient.InternalRequestSender.run(AsynchronousRequestSender.java:138)
09-26 13:33:29.571: E/AndroidRuntime(32300):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-26 13:33:29.571: E/AndroidRuntime(32300):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-26 13:33:29.571: E/AndroidRuntime(32300):    at java.lang.Thread.run(Thread.java:841)

1条回答
够拽才男人
2楼-- · 2019-08-04 05:00

The current implementation is not ideal. If you'd like, you can submit a feature request to have this re-evaluated for a future release. You can submit it via: http://www.ibm.com/developerworks/rfe/

The reason it is like this, is because WLClient has some features, such as displaying UI error messages, monitoring whether the app is in the foreground/background for heartbeat as well as push notifications support, that require an Android context.

In this case, the error you are getting is expected.

查看更多
登录 后发表回答