26
26
import com .google .androidbrowserhelper .trusted .TwaLauncher ;
27
27
import com .google .androidbrowserhelper .trusted .TwaProviderPicker ;
28
28
29
+ import java .util .ArrayList ;
29
30
import java .util .Arrays ;
30
31
import java .util .List ;
31
32
@@ -42,6 +43,11 @@ public class LaunchTwaActivity extends AppCompatActivity {
42
43
private final TrustedWebActivityIntentBuilder builder = new TrustedWebActivityIntentBuilder (
43
44
LAUNCH_URI );
44
45
46
+ /**
47
+ * A bag to put all TwaLauncher in so we can dispose all at once.
48
+ */
49
+ private List <TwaLauncher > launchers = new ArrayList <>();
50
+
45
51
private final CustomTabsServiceConnection customTabsServiceConnection = new CustomTabsServiceConnection () {
46
52
CustomTabsSession mSession ;
47
53
private final static int SESSION_ID = 45 ; // An arbitrary constant.
@@ -82,7 +88,9 @@ protected void onCreate(Bundle savedInstanceState) {
82
88
* @param view the source of the event invoking this method.
83
89
*/
84
90
public void launch (View view ) {
85
- new TwaLauncher (this ).launch (LAUNCH_URI );
91
+ TwaLauncher launcher = new TwaLauncher (this );
92
+ launcher .launch (LAUNCH_URI );
93
+ launchers .add (launcher );
86
94
}
87
95
88
96
/**
@@ -96,7 +104,10 @@ public void launchWithCustomColors(View view) {
96
104
.setNavigationBarColor (Color .RED )
97
105
.setToolbarColor (Color .BLUE );
98
106
99
- new TwaLauncher (this ).launch (builder , null , null );
107
+
108
+ TwaLauncher launcher = new TwaLauncher (this );
109
+ launcher .launch (builder , null , null );
110
+ launchers .add (launcher );
100
111
}
101
112
102
113
/**
@@ -113,7 +124,10 @@ public void launcherWithMultipleOrigins(View view) {
113
124
TrustedWebActivityIntentBuilder builder = new TrustedWebActivityIntentBuilder (LAUNCH_URI )
114
125
.setAdditionalTrustedOrigins (origins );
115
126
116
- new TwaLauncher (this ).launch (builder , null , null );
127
+
128
+ TwaLauncher launcher = new TwaLauncher (this );
129
+ launcher .launch (builder , null , null );
130
+ launchers .add (launcher );
117
131
}
118
132
119
133
/**
@@ -136,6 +150,9 @@ public void launchWithCustomReferrer(View view) {
136
150
@ Override
137
151
protected void onDestroy () {
138
152
super .onDestroy ();
153
+ for (TwaLauncher launcher : launchers ) {
154
+ launcher .destroy ();
155
+ }
139
156
if (serviceBound ) {
140
157
unbindService (customTabsServiceConnection );
141
158
serviceBound = false ;
0 commit comments