Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,26 @@ public class BrowserControllerActivity extends ComponentActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// close() starts a new instance when the previous one was already cleared off the task
// (e.g. by a deep link into a singleTask activity) but not destroyed yet. There is nothing
// to close then, and the last open() must not run again.
if (getIntent().hasExtra("close")) {
finish();
return;
}

customTabLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), (result) -> {
if (implementation != null) {
implementation.notifyBrowserFinished();
}
finish();
});

if (BrowserPlugin.browserControllerListener != null) {
BrowserPlugin.browserControllerListener.onControllerReady(this);
// Each open() is served once, by the controller it started.
BrowserControllerListener listener = BrowserPlugin.browserControllerListener;
BrowserPlugin.browserControllerListener = null;
if (listener != null) {
listener.onControllerReady(this);
}
}

Expand Down Expand Up @@ -64,6 +75,6 @@ public void open(Browser implementation, Uri url, Integer toolbarColor) {
protected void onDestroy() {
super.onDestroy();
implementation = null;
BrowserPlugin.setBrowserControllerListener(null);
BrowserPlugin.onControllerDestroyed(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public class BrowserPlugin extends Plugin {

private Browser implementation;

public static BrowserControllerListener browserControllerListener;
private static BrowserControllerActivity browserControllerActivityInstance;
// Written on the plugin thread, read on the main thread.
public static volatile BrowserControllerListener browserControllerListener;
private static volatile BrowserControllerActivity browserControllerActivityInstance;

public static void setBrowserControllerListener(BrowserControllerListener listener) {
browserControllerListener = listener;
Expand All @@ -25,6 +26,16 @@ public static void setBrowserControllerListener(BrowserControllerListener listen
}
}

/**
* Called by each controller as it is destroyed. It forgets that controller only: the listener
* may already belong to a newer open() whose controller has not been created yet.
*/
static void onControllerDestroyed(BrowserControllerActivity activity) {
if (browserControllerActivityInstance == activity) {
browserControllerActivityInstance = null;
}
}

public void load() {
implementation = new Browser(getContext());
implementation.setBrowserEventListener(this::onBrowserEvent);
Expand Down Expand Up @@ -61,11 +72,7 @@ public void open(PluginCall call) {

// open the browser and finish

Intent intent = new Intent(getContext(), BrowserControllerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(intent);

// Set the listener before starting the activity, so its onCreate can't run first and miss it.
Integer finalToolbarColor = toolbarColor;
setBrowserControllerListener((activity) -> {
try {
Expand All @@ -77,6 +84,11 @@ public void open(PluginCall call) {
call.reject("Unable to display URL");
}
});

Intent intent = new Intent(getContext(), BrowserControllerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(intent);
}

@PluginMethod
Expand Down