Skip to content

Commit

Permalink
Fix memory leak in CustomerSessionActivity (#960)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshafrir-stripe authored May 20, 2019
1 parent c07535e commit 0e1f958
Showing 1 changed file with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.stripe.example.controller.ErrorDialogHandler;
import com.stripe.example.service.ExampleEphemeralKeyProvider;

import java.lang.ref.WeakReference;

/**
* An example activity that handles working with a {@link CustomerSession}, allowing you to
* add and select sources for the current customer.
Expand All @@ -44,12 +46,7 @@ protected void onCreate(Bundle savedInstanceState) {
mSelectSourceButton.setEnabled(false);
mErrorDialogHandler = new ErrorDialogHandler(this);
CustomerSession.initCustomerSession(this,
new ExampleEphemeralKeyProvider(
string -> {
if (string.startsWith("Error: ")) {
mErrorDialogHandler.show(string);
}
}));
new ExampleEphemeralKeyProvider(new ProgressListenerImpl(this)));

mProgressBar.setVisibility(View.VISIBLE);
CustomerSession.getInstance().retrieveCurrentCustomer(
Expand Down Expand Up @@ -115,4 +112,22 @@ public void onError(int httpCode, @Nullable String errorMessage,
}
}
}

private static final class ProgressListenerImpl
implements ExampleEphemeralKeyProvider.ProgressListener {

@NonNull private final WeakReference<CustomerSessionActivity> mActivityRef;

private ProgressListenerImpl(@NonNull CustomerSessionActivity activity) {
this.mActivityRef = new WeakReference<>(activity);
}

@Override
public void onStringResponse(@NonNull String response) {
final CustomerSessionActivity activity = mActivityRef.get();
if (activity != null && response.startsWith("Error: ")) {
activity.mErrorDialogHandler.show(response);
}
}
}
}

0 comments on commit 0e1f958

Please sign in to comment.