2
我在我的一個片段中使用Android Pay。這裏是我的片段如何(相關部分)如下:onSaveInstanceState |後無法執行操作AndroidPay
public class PaymentsFragment extends Fragment implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
private MaskedWallet mMaskedWallet;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addOnConnectionFailedListener(this)
.addApi(Wallet.API, new Wallet.WalletOptions.Builder()
.setEnvironment(BuildConfig.DEBUG ? WalletConstants.ENVIRONMENT_TEST : WalletConstants.ENVIRONMENT_PRODUCTION)
.setTheme(WalletConstants.THEME_LIGHT)
.build())
.enableAutoManage(getActivity(), this)
.build();
Wallet.Payments.isReadyToPay(mGoogleApiClient).setResultCallback(
new ResultCallback<BooleanResult>() {
@Override
public void onResult(@NonNull BooleanResult booleanResult) {
if (booleanResult.getStatus().isSuccess()) {
if (booleanResult.getValue()) {
showAndroidPay();
}
}
}
});
}
private void showAndroidPay() {
WalletFragmentStyle walletFragmentStyle = new WalletFragmentStyle()
.setBuyButtonText(WalletFragmentStyle.BuyButtonText.LOGO_ONLY)
.setBuyButtonAppearance(WalletFragmentStyle.BuyButtonAppearance.ANDROID_PAY_LIGHT)
.setBuyButtonHeight(WalletFragmentStyle.Dimension.UNIT_DIP, 46);
WalletFragmentOptions walletFragmentOptions = WalletFragmentOptions.newBuilder()
.setEnvironment(BuildConfig.DEBUG ? WalletConstants.ENVIRONMENT_TEST : WalletConstants.ENVIRONMENT_PRODUCTION)
.setFragmentStyle(walletFragmentStyle)
.setTheme(WalletConstants.THEME_LIGHT)
.setMode(WalletFragmentMode.BUY_BUTTON)
.build();
PaymentMethodTokenizationParameters params = null;
if (requestMethod.getGateway().getType() == PaymentGateway.PaymentGatewayType.STRIPE) {
params = PaymentMethodTokenizationParameters.newBuilder()
.setPaymentMethodTokenizationType(PaymentMethodTokenizationType.PAYMENT_GATEWAY)
.addParameter(GOOGLE_WALLET_GATEWAY, PaymentGateway.PaymentGatewayType.STRIPE.toString().toLowerCase())
.addParameter(GOOGLE_WALLET_STRIPE_KEY, ((StripePaymentGateway) requestMethod.getGateway()).getPublishableKey())
.addParameter(GOOGLE_WALLET_STRIPE_VERSION, com.stripe.Stripe.VERSION)
.build();
} else {
//later
}
MaskedWalletRequest maskedWalletRequest = MaskedWalletRequest.newBuilder()
.setMerchantName("")
.setPaymentMethodTokenizationParameters(params)
.setPhoneNumberRequired(false)
.setShippingAddressRequired(false)
.setEstimatedTotalPrice(100.0)
.setCurrencyCode("USD")
.build();
WalletFragmentInitParams initParams = WalletFragmentInitParams.newBuilder()
.setMaskedWalletRequest(maskedWalletRequest)
.setMaskedWalletRequestCode(LOADER_MASKED_WALLET_REQUEST_CODE)
.build();
SupportWalletFragment walletFragment = SupportWalletFragment.newInstance(walletFragmentOptions);
walletFragment.initialize(initParams);
if (walletFragment != null) {
getFragmentManager().beginTransaction()
.replace(R.id.androidpay_fragment_holder, walletFragment)
.commit();
}
}
}
現在,當我嘗試點擊AndroidPay按鈕,應用程序崩潰,出現異常後,該裝置旋轉:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
在我致力於showAndroidPay()
方法的時候。我知道這article其中說,不應該有異步方法提交FragmentTransaction
。但在我的情況下,當我從回調中收到成功時,我只能顯示Android Pay選項。這就是即使sample app這樣做。唯一的區別是它在Activity.onCreate()
與Fragment.onCreaveView()
在我的情況。
任何想法如何解決這個問題?
是的,我知道這個解決方案。我覺得這更像是一種黑客。 – Nerd