Skip to content

Commit

Permalink
Workaround for activity stateloss
Browse files Browse the repository at this point in the history
  • Loading branch information
jberkel committed Apr 19, 2018
1 parent e0d68b2 commit 27bfc4f
Showing 1 changed file with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.zegoggles.smssync.activity.donation;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClient.BillingResponse;
Expand Down Expand Up @@ -61,6 +59,7 @@ enum State {

private static boolean DEBUG_IAB = BuildConfig.DEBUG;
private @Nullable BillingClient billingClient;
private boolean stateSaved;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -95,14 +94,25 @@ protected void onDestroy() {
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
stateSaved = true;
}

@Override
public void onSkuDetailsResponse(@BillingResponse int responseCode, List<SkuDetails> details) {
log("onSkuDetailsResponse(" + responseCode + ", " + details + ")");
if (isFinishing() || responseCode != OK) {
if (responseCode != OK) {
Log.w(TAG, "failed to query inventory: " + responseCode);
return;
}

if (isFinishing() || stateSaved) {
Log.w(TAG, "activity no longer active");
return;
}

List<SkuDetails> skuDetailsList = new ArrayList<SkuDetails>();
for (SkuDetails d : details) {
if (d.getSku().startsWith(DONATION_PREFIX)) {
Expand Down Expand Up @@ -155,19 +165,6 @@ public void selectedSku(String sku) {
);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
// 🤔 suggested workaround from
// https://issuetracker.google.com/issues/37094575
super.onSaveInstanceState(outState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final View rootView = findViewById(android.R.id.content);
if (rootView != null) {
rootView.cancelPendingInputEvents();
}
}
}

private void queryAvailableSkus() {
if (billingClient == null) return;
billingClient.querySkuDetailsAsync(SkuDetailsParams.newBuilder()
Expand Down

0 comments on commit 27bfc4f

Please sign in to comment.