|
1 | 1 | package io.snabble.sdk.ui.checkout; |
2 | 2 |
|
| 3 | +import android.app.Activity; |
| 4 | +import android.app.Application; |
3 | 5 | import android.app.ProgressDialog; |
4 | 6 | import android.content.Context; |
5 | 7 | import android.content.DialogInterface; |
|
16 | 18 | import io.snabble.sdk.ui.R; |
17 | 19 | import io.snabble.sdk.ui.SnabbleUI; |
18 | 20 | import io.snabble.sdk.ui.utils.DelayedProgressDialog; |
| 21 | +import io.snabble.sdk.ui.utils.UIUtils; |
| 22 | +import io.snabble.sdk.utils.SimpleActivityLifecycleCallbacks; |
19 | 23 |
|
20 | 24 | public class CheckoutView extends FrameLayout implements Checkout.OnCheckoutStateChangedListener { |
21 | 25 | private ViewAnimator viewAnimator; |
@@ -126,22 +130,52 @@ public void displayView(View view) { |
126 | 130 | ViewGroup.LayoutParams.MATCH_PARENT)); |
127 | 131 | } |
128 | 132 |
|
| 133 | + public void registerListeners() { |
| 134 | + checkout.addOnCheckoutStateChangedListener(this); |
| 135 | + progressDialog.setOnCancelListener(onCancelListener); |
| 136 | + } |
| 137 | + |
| 138 | + public void unregisterListeners() { |
| 139 | + checkout.removeOnCheckoutStateChangedListener(this); |
| 140 | + |
| 141 | + progressDialog.setOnCancelListener(null); |
| 142 | + progressDialog.dismiss(); |
| 143 | + } |
| 144 | + |
129 | 145 | @Override |
130 | | - protected void onAttachedToWindow() { |
| 146 | + public void onAttachedToWindow() { |
131 | 147 | super.onAttachedToWindow(); |
132 | 148 |
|
133 | | - checkout.addOnCheckoutStateChangedListener(this); |
| 149 | + Application application = (Application) getContext().getApplicationContext(); |
| 150 | + application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks); |
134 | 151 |
|
135 | | - progressDialog.setOnCancelListener(onCancelListener); |
| 152 | + registerListeners(); |
136 | 153 | } |
137 | 154 |
|
138 | 155 | @Override |
139 | | - protected void onDetachedFromWindow() { |
| 156 | + public void onDetachedFromWindow() { |
140 | 157 | super.onDetachedFromWindow(); |
141 | 158 |
|
142 | | - checkout.removeOnCheckoutStateChangedListener(this); |
| 159 | + Application application = (Application) getContext().getApplicationContext(); |
| 160 | + application.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks); |
143 | 161 |
|
144 | | - progressDialog.setOnCancelListener(null); |
145 | | - progressDialog.dismiss(); |
| 162 | + unregisterListeners(); |
146 | 163 | } |
| 164 | + |
| 165 | + private Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = |
| 166 | + new SimpleActivityLifecycleCallbacks() { |
| 167 | + @Override |
| 168 | + public void onActivityStarted(Activity activity) { |
| 169 | + if (UIUtils.getHostActivity(getContext()) == activity) { |
| 170 | + registerListeners(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void onActivityStopped(Activity activity) { |
| 176 | + if (UIUtils.getHostActivity(getContext()) == activity) { |
| 177 | + unregisterListeners(); |
| 178 | + } |
| 179 | + } |
| 180 | + }; |
147 | 181 | } |
0 commit comments