Skip to content

Commit 232a24d

Browse files
committed
Added onPause callback for Android
1 parent 8969620 commit 232a24d

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

android/src/main/java/com/pspdfkit/flutter/pspdfkit/FlutterPdfActivity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import java.util.concurrent.atomic.AtomicReference;
1111

12+
import io.flutter.plugin.common.MethodChannel;
13+
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
1214
import io.flutter.plugin.common.MethodChannel.Result;
1315

1416
/**
@@ -18,18 +20,29 @@
1820
public class FlutterPdfActivity extends PdfActivity {
1921

2022
private static FlutterPdfActivity currentActivity;
23+
private static MethodChannel channel;
2124
private static AtomicReference<Result> loadedDocumentResult = new AtomicReference<>();
2225

2326
public static void setLoadedDocumentResult(Result result) {
2427
loadedDocumentResult.set(result);
2528
}
2629

30+
public static void setMethodChannel(MethodChannel methodChannel) {
31+
channel = methodChannel;
32+
}
33+
2734
@Override
2835
protected void onCreate(Bundle bundle) {
2936
super.onCreate(bundle);
3037
bindActivity();
3138
}
3239

40+
@Override
41+
protected void onPause() {
42+
super.onPause();
43+
channel.invokeMethod("flutterPdfActivityOnPause", null, null);
44+
}
45+
3346
@Override
3447
protected void onDestroy() {
3548
super.onDestroy();

android/src/main/java/com/pspdfkit/flutter/pspdfkit/PspdfkitPlugin.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class PspdfkitPlugin implements MethodCallHandler, PluginRegistry.Request
6363
private static final String FILE_SCHEME = "file:///";
6464
private final Context context;
6565
private final Registrar registrar;
66+
private static MethodChannel channel;
6667
/** Atomic reference that prevents sending twice the permission result and throwing exception. */
6768
private AtomicReference<Result> permissionRequestResult;
6869

@@ -76,7 +77,7 @@ private PspdfkitPlugin(Registrar registrar) {
7677
* Plugin registration.
7778
*/
7879
public static void registerWith(Registrar registrar) {
79-
final MethodChannel channel = new MethodChannel(registrar.messenger(), "pspdfkit");
80+
channel = new MethodChannel(registrar.messenger(), "pspdfkit");
8081
PspdfkitPlugin pspdfkitPlugin = new PspdfkitPlugin(registrar);
8182
channel.setMethodCallHandler(pspdfkitPlugin);
8283
registrar.addRequestPermissionsResultListener(pspdfkitPlugin);
@@ -88,6 +89,8 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
8889
FlutterPdfActivity flutterPdfActivity;
8990
PdfDocument document;
9091

92+
FlutterPdfActivity.setMethodChannel(channel);
93+
9194
switch (call.method) {
9295
case "frameworkVersion":
9396
result.success("Android " + PSPDFKit.VERSION);

example/lib/main.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
268268
Pspdfkit.setLicenseKey("LICENSE_KEY_GOES_HERE");
269269
}
270270

271+
void flutterPdfActivityOnPauseHandler() {
272+
print("flutterPdfActivityOnPauseHandler");
273+
}
274+
271275
void pdfViewControllerWillDismissHandler() {
272276
print("pdfViewControllerWillDismissHandler");
273277
}
@@ -279,6 +283,7 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
279283
@override
280284
Widget build(BuildContext context) {
281285
Pspdfkit.setupPlatformCallHandler();
286+
Pspdfkit.flutterPdfActivityOnPause = () => flutterPdfActivityOnPauseHandler();
282287
Pspdfkit.pdfViewControllerWillDismiss = () => pdfViewControllerWillDismissHandler();
283288
Pspdfkit.pdfViewControllerDidDismiss = () => pdfViewControllerDidDismissHandler();
284289

lib/pspdfkit.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,16 @@ class Pspdfkit {
9595
}
9696
}
9797

98+
static VoidCallback flutterPdfActivityOnPause;
9899
static VoidCallback pdfViewControllerWillDismiss;
99100
static VoidCallback pdfViewControllerDidDismiss;
100101

101102
static Future<void> _platformCallHandler(MethodCall call) {
102103
try {
103104
switch (call.method) {
105+
case 'flutterPdfActivityOnPause':
106+
flutterPdfActivityOnPause();
107+
break;
104108
case 'pdfViewControllerWillDismiss':
105109
pdfViewControllerWillDismiss();
106110
break;

0 commit comments

Comments
 (0)