最经在做关于android in-app Billing的功能,遇到一个异常,activity不会执行OnActivityResult()方法,网上搜了半天也没发现有什么好的解决方法,今天终于在一段异常信息中看出了原因。
异常信息如下:
couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView
这才发现,子Activity在ActivityGroup中不会执行OnActivityResult()方法,导致in-app billing接受不到google server发送的信息,当前的资源和线程得不到关闭,
导致出现异常: java.lang.IllegalStateException: Can't start async operation (launchPurchaseFlow) because another async operation(launchPurchaseFlow) is in progress.
解决办法如下:
1.通过父Activity启动其他Activity;
2.实现父Activity的onActivityResult方法,将该处接收到的请求转发给当前活动的子Activity;
3.自定义一个转发接口用来实现第2步中的转发功能;
4.子Activity中实现第3步接口中的方法用来接收返回信息;
代码如下:
代码如下:
1.通过父Activity启动其他Activity
getParent().startActivityForResult(new Intent(...), REQUEST_CODE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 获取当前活动的Activity实例
Activity subActivity = getLocalActivityManager().getCurrentActivity();
//判断是否实现返回值接口
if (subActivity instanceof OnTabActivityResultListener) {
//获取返回值接口实例
OnTabActivityResultListener listener = (OnTabActivityResultListener) subActivity;
//转发请求到子Activity
listener.onTabActivityResult(requestCode, resultCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 解决子Activity无法接收Activity回调的问题
* @author Administrator
*
*/
public interface OnTabActivityResultListener {
public void onTabActivityResult(int requestCode, int resultCode, Intent data);
}
4.子Activity需要实现onTabActivityResult方法
@Override
public void onTabActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
//实现该处逻辑
...
}
}
这样就解决了 in-app billing
java.lang.IllegalStateException: Can't start async operation (launchPurchaseFlow) because another async operation(launchPurchaseFlow) is in progress.
的问题。