RxBus.getDefault().subscribe(this, QuantManager.DK_SHARE_MEDIA, RxBus.Callback<String> { s ->
if ("1" == s) {
requestDKShare()
}
})
原来的分享成功会刷新接口,并弹窗
private fun requestDKShare() {
val params = ArrayList<KeyValueData>()
params.add(KeyValueData("userToken", UserManager.userToken()))
params.add(KeyValueData("action", "share"))
params.add(KeyValueData("courseid", "4357"))
RxHttp.call(RequestCommand.COMMAND_DK_SHARE, params, QuantDkActiveResponse::class.java) { data ->
refreshData()
QuantDkMainDialog.newInstance(QuantDkMainDialog.TYPE_SHARE, data.message).show(supportFragmentManager, "QuantDkMainDialog")
}
}
改成局部变量的形式, transaction.commitAllowingStateLoss() 防止崩溃
private fun requestDKShare() {
val params = ArrayList<KeyValueData>()
params.add(KeyValueData("userToken", UserManager.userToken()))
params.add(KeyValueData("action", "share"))
params.add(KeyValueData("courseid", "4357"))
RxHttp.call(RequestCommand.COMMAND_DK_SHARE, params, QuantDkActiveResponse::class.java) { data ->
refreshData()
val dialog = QuantDkMainDialog.newInstance(QuantDkMainDialog.TYPE_SHARE, data.message)
val transaction = supportFragmentManager.beginTransaction()
transaction.add(dialog, "dialog")
transaction.commitAllowingStateLoss()
}
}
相对于 qq,微博,等其他分享,微信分享 会回调WXEntryActivity类,这样QuantDKHomePlusActivity类就进入后台,supportFragmentManager就获取不到了,所以会引起崩溃
另外,只要微信分享获取不到
private UMShareListener shareListener = new UMShareListener() {
@Override
public void onStart(SHARE_MEDIA platform) {
}
@Override
public void onResult(SHARE_MEDIA platform) {
if (context instanceof QuantDKHomePlusActivity) {
RxBus.getDefault().post(QuantManager.DK_SHARE_MEDIA, "1");
} else if (context instanceof QuantDKDetailPlusActivity) {
RxBus.getDefault().post(QuantManager.DK_SHARE_MEDIA, "2");
}
Toast.makeText(context,"分享成功",Toast.LENGTH_LONG).show();
}
@Override
public void onError(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(context,"分享失败"+t.getMessage(),Toast.LENGTH_LONG).show();
}
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(context,"取消分享",Toast.LENGTH_LONG).show();
}
};
需要在WXEntryActivity类中如下处理
@Override
public void onResp(BaseResp resp) {
switch (resp.errCode) {
case BaseResp.ErrCode.ERR_OK:
if (resp instanceof SendAuth.Resp) {
String code = ((SendAuth.Resp) resp).code;
Intent intent = new Intent();
intent.putExtra("wxCode", code);
intent.setAction("wx_login");
sendBroadcast(intent);
} else {
if (this.mWxHandler != null) {
try {
this.mWxHandler.getWXEventHandler().onResp(resp);
} catch (Exception var3) {
SLog.error(var3);
}
}
ToastTool.showToast("分享成功");
}
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
if (resp.getType() == 1) {
ToastTool.showToast("取消登录");
} else if (resp.getType() == 2) {
ToastTool.showToast("取消分享");
} else {
}
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:
ToastTool.showToast("授权失败");
break;
default:
break;
}
finish();
}
其实调用的是 super.onResp,但这样就会影响第三方登录,所以只在分享时调用
if (this.mWxHandler != null) {
try {
this.mWxHandler.getWXEventHandler().onResp(resp);
} catch (Exception var3) {
SLog.error(var3);
}
}
WXCallbackActivity 微信回调类的 onResp方法
public void onResp(BaseResp resp) {
SLog.I("WXCallbackActivity 分发回调");
if (this.mWxHandler != null && resp != null) {
try {
this.mWxHandler.getWXEventHandler().onResp(resp);
} catch (Exception var3) {
SLog.error(var3);
}
}
this.finish();
}